datapath.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385
  1. /*
  2. * Copyright (c) 2007-2012 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/lockdep.h>
  46. #include <linux/openvswitch.h>
  47. #include <linux/rculist.h>
  48. #include <linux/dmi.h>
  49. #include <linux/workqueue.h>
  50. #include <net/genetlink.h>
  51. #include <net/net_namespace.h>
  52. #include <net/netns/generic.h>
  53. #include "datapath.h"
  54. #include "flow.h"
  55. #include "vport-internal_dev.h"
  56. #include "vport-netdev.h"
  57. #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
  58. static void rehash_flow_table(struct work_struct *work);
  59. static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
  60. int ovs_net_id __read_mostly;
  61. static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
  62. struct genl_multicast_group *grp)
  63. {
  64. genl_notify(skb, genl_info_net(info), info->snd_portid,
  65. grp->id, info->nlhdr, GFP_KERNEL);
  66. }
  67. /**
  68. * DOC: Locking:
  69. *
  70. * All writes e.g. Writes to device state (add/remove datapath, port, set
  71. * operations on vports, etc.), Writes to other state (flow table
  72. * modifications, set miscellaneous datapath parameters, etc.) are protected
  73. * by ovs_lock.
  74. *
  75. * Reads are protected by RCU.
  76. *
  77. * There are a few special cases (mostly stats) that have their own
  78. * synchronization but they nest under all of above and don't interact with
  79. * each other.
  80. *
  81. * The RTNL lock nests inside ovs_mutex.
  82. */
  83. static DEFINE_MUTEX(ovs_mutex);
  84. void ovs_lock(void)
  85. {
  86. mutex_lock(&ovs_mutex);
  87. }
  88. void ovs_unlock(void)
  89. {
  90. mutex_unlock(&ovs_mutex);
  91. }
  92. #ifdef CONFIG_LOCKDEP
  93. int lockdep_ovsl_is_held(void)
  94. {
  95. if (debug_locks)
  96. return lockdep_is_held(&ovs_mutex);
  97. else
  98. return 1;
  99. }
  100. #endif
  101. static struct vport *new_vport(const struct vport_parms *);
  102. static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
  103. const struct dp_upcall_info *);
  104. static int queue_userspace_packet(struct net *, int dp_ifindex,
  105. struct sk_buff *,
  106. const struct dp_upcall_info *);
  107. /* Must be called with rcu_read_lock or ovs_mutex. */
  108. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  109. {
  110. struct datapath *dp = NULL;
  111. struct net_device *dev;
  112. rcu_read_lock();
  113. dev = dev_get_by_index_rcu(net, dp_ifindex);
  114. if (dev) {
  115. struct vport *vport = ovs_internal_dev_get_vport(dev);
  116. if (vport)
  117. dp = vport->dp;
  118. }
  119. rcu_read_unlock();
  120. return dp;
  121. }
  122. /* Must be called with rcu_read_lock or ovs_mutex. */
  123. const char *ovs_dp_name(const struct datapath *dp)
  124. {
  125. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  126. return vport->ops->get_name(vport);
  127. }
  128. static int get_dpifindex(struct datapath *dp)
  129. {
  130. struct vport *local;
  131. int ifindex;
  132. rcu_read_lock();
  133. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  134. if (local)
  135. ifindex = netdev_vport_priv(local)->dev->ifindex;
  136. else
  137. ifindex = 0;
  138. rcu_read_unlock();
  139. return ifindex;
  140. }
  141. static void destroy_dp_rcu(struct rcu_head *rcu)
  142. {
  143. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  144. ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
  145. free_percpu(dp->stats_percpu);
  146. release_net(ovs_dp_get_net(dp));
  147. kfree(dp->ports);
  148. kfree(dp);
  149. }
  150. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  151. u16 port_no)
  152. {
  153. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  154. }
  155. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  156. {
  157. struct vport *vport;
  158. struct hlist_head *head;
  159. head = vport_hash_bucket(dp, port_no);
  160. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  161. if (vport->port_no == port_no)
  162. return vport;
  163. }
  164. return NULL;
  165. }
  166. /* Called with ovs_mutex. */
  167. static struct vport *new_vport(const struct vport_parms *parms)
  168. {
  169. struct vport *vport;
  170. vport = ovs_vport_add(parms);
  171. if (!IS_ERR(vport)) {
  172. struct datapath *dp = parms->dp;
  173. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  174. hlist_add_head_rcu(&vport->dp_hash_node, head);
  175. }
  176. return vport;
  177. }
  178. void ovs_dp_detach_port(struct vport *p)
  179. {
  180. ASSERT_OVSL();
  181. /* First drop references to device. */
  182. hlist_del_rcu(&p->dp_hash_node);
  183. /* Then destroy it. */
  184. ovs_vport_del(p);
  185. }
  186. /* Must be called with rcu_read_lock. */
  187. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  188. {
  189. struct datapath *dp = p->dp;
  190. struct sw_flow *flow;
  191. struct dp_stats_percpu *stats;
  192. struct sw_flow_key key;
  193. u64 *stats_counter;
  194. int error;
  195. int key_len;
  196. stats = this_cpu_ptr(dp->stats_percpu);
  197. /* Extract flow from 'skb' into 'key'. */
  198. error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
  199. if (unlikely(error)) {
  200. kfree_skb(skb);
  201. return;
  202. }
  203. /* Look up flow. */
  204. flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
  205. if (unlikely(!flow)) {
  206. struct dp_upcall_info upcall;
  207. upcall.cmd = OVS_PACKET_CMD_MISS;
  208. upcall.key = &key;
  209. upcall.userdata = NULL;
  210. upcall.portid = p->upcall_portid;
  211. ovs_dp_upcall(dp, skb, &upcall);
  212. consume_skb(skb);
  213. stats_counter = &stats->n_missed;
  214. goto out;
  215. }
  216. OVS_CB(skb)->flow = flow;
  217. stats_counter = &stats->n_hit;
  218. ovs_flow_used(OVS_CB(skb)->flow, skb);
  219. ovs_execute_actions(dp, skb);
  220. out:
  221. /* Update datapath statistics. */
  222. u64_stats_update_begin(&stats->sync);
  223. (*stats_counter)++;
  224. u64_stats_update_end(&stats->sync);
  225. }
  226. static struct genl_family dp_packet_genl_family = {
  227. .id = GENL_ID_GENERATE,
  228. .hdrsize = sizeof(struct ovs_header),
  229. .name = OVS_PACKET_FAMILY,
  230. .version = OVS_PACKET_VERSION,
  231. .maxattr = OVS_PACKET_ATTR_MAX,
  232. .netnsok = true,
  233. .parallel_ops = true,
  234. };
  235. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  236. const struct dp_upcall_info *upcall_info)
  237. {
  238. struct dp_stats_percpu *stats;
  239. int dp_ifindex;
  240. int err;
  241. if (upcall_info->portid == 0) {
  242. err = -ENOTCONN;
  243. goto err;
  244. }
  245. dp_ifindex = get_dpifindex(dp);
  246. if (!dp_ifindex) {
  247. err = -ENODEV;
  248. goto err;
  249. }
  250. if (!skb_is_gso(skb))
  251. err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  252. else
  253. err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  254. if (err)
  255. goto err;
  256. return 0;
  257. err:
  258. stats = this_cpu_ptr(dp->stats_percpu);
  259. u64_stats_update_begin(&stats->sync);
  260. stats->n_lost++;
  261. u64_stats_update_end(&stats->sync);
  262. return err;
  263. }
  264. static int queue_gso_packets(struct net *net, int dp_ifindex,
  265. struct sk_buff *skb,
  266. const struct dp_upcall_info *upcall_info)
  267. {
  268. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  269. struct dp_upcall_info later_info;
  270. struct sw_flow_key later_key;
  271. struct sk_buff *segs, *nskb;
  272. int err;
  273. segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
  274. if (IS_ERR(segs))
  275. return PTR_ERR(segs);
  276. /* Queue all of the segments. */
  277. skb = segs;
  278. do {
  279. err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
  280. if (err)
  281. break;
  282. if (skb == segs && gso_type & SKB_GSO_UDP) {
  283. /* The initial flow key extracted by ovs_flow_extract()
  284. * in this case is for a first fragment, so we need to
  285. * properly mark later fragments.
  286. */
  287. later_key = *upcall_info->key;
  288. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  289. later_info = *upcall_info;
  290. later_info.key = &later_key;
  291. upcall_info = &later_info;
  292. }
  293. } while ((skb = skb->next));
  294. /* Free all of the segments. */
  295. skb = segs;
  296. do {
  297. nskb = skb->next;
  298. if (err)
  299. kfree_skb(skb);
  300. else
  301. consume_skb(skb);
  302. } while ((skb = nskb));
  303. return err;
  304. }
  305. static size_t key_attr_size(void)
  306. {
  307. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  308. + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
  309. + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
  310. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
  311. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
  312. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
  313. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
  314. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
  315. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
  316. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  317. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  318. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  319. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  320. + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
  321. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  322. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  323. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  324. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  325. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  326. }
  327. static size_t upcall_msg_size(const struct sk_buff *skb,
  328. const struct nlattr *userdata)
  329. {
  330. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  331. + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
  332. + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  333. /* OVS_PACKET_ATTR_USERDATA */
  334. if (userdata)
  335. size += NLA_ALIGN(userdata->nla_len);
  336. return size;
  337. }
  338. static int queue_userspace_packet(struct net *net, int dp_ifindex,
  339. struct sk_buff *skb,
  340. const struct dp_upcall_info *upcall_info)
  341. {
  342. struct ovs_header *upcall;
  343. struct sk_buff *nskb = NULL;
  344. struct sk_buff *user_skb; /* to be queued to userspace */
  345. struct nlattr *nla;
  346. int err;
  347. if (vlan_tx_tag_present(skb)) {
  348. nskb = skb_clone(skb, GFP_ATOMIC);
  349. if (!nskb)
  350. return -ENOMEM;
  351. nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
  352. if (!nskb)
  353. return -ENOMEM;
  354. nskb->vlan_tci = 0;
  355. skb = nskb;
  356. }
  357. if (nla_attr_size(skb->len) > USHRT_MAX) {
  358. err = -EFBIG;
  359. goto out;
  360. }
  361. user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
  362. if (!user_skb) {
  363. err = -ENOMEM;
  364. goto out;
  365. }
  366. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  367. 0, upcall_info->cmd);
  368. upcall->dp_ifindex = dp_ifindex;
  369. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  370. ovs_flow_to_nlattrs(upcall_info->key, user_skb);
  371. nla_nest_end(user_skb, nla);
  372. if (upcall_info->userdata)
  373. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  374. nla_len(upcall_info->userdata),
  375. nla_data(upcall_info->userdata));
  376. nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
  377. skb_copy_and_csum_dev(skb, nla_data(nla));
  378. genlmsg_end(user_skb, upcall);
  379. err = genlmsg_unicast(net, user_skb, upcall_info->portid);
  380. out:
  381. kfree_skb(nskb);
  382. return err;
  383. }
  384. /* Called with ovs_mutex. */
  385. static int flush_flows(struct datapath *dp)
  386. {
  387. struct flow_table *old_table;
  388. struct flow_table *new_table;
  389. old_table = ovsl_dereference(dp->table);
  390. new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
  391. if (!new_table)
  392. return -ENOMEM;
  393. rcu_assign_pointer(dp->table, new_table);
  394. ovs_flow_tbl_deferred_destroy(old_table);
  395. return 0;
  396. }
  397. static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
  398. {
  399. struct sw_flow_actions *acts;
  400. int new_acts_size;
  401. int req_size = NLA_ALIGN(attr_len);
  402. int next_offset = offsetof(struct sw_flow_actions, actions) +
  403. (*sfa)->actions_len;
  404. if (req_size <= (ksize(*sfa) - next_offset))
  405. goto out;
  406. new_acts_size = ksize(*sfa) * 2;
  407. if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
  408. if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
  409. return ERR_PTR(-EMSGSIZE);
  410. new_acts_size = MAX_ACTIONS_BUFSIZE;
  411. }
  412. acts = ovs_flow_actions_alloc(new_acts_size);
  413. if (IS_ERR(acts))
  414. return (void *)acts;
  415. memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
  416. acts->actions_len = (*sfa)->actions_len;
  417. kfree(*sfa);
  418. *sfa = acts;
  419. out:
  420. (*sfa)->actions_len += req_size;
  421. return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
  422. }
  423. static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
  424. {
  425. struct nlattr *a;
  426. a = reserve_sfa_size(sfa, nla_attr_size(len));
  427. if (IS_ERR(a))
  428. return PTR_ERR(a);
  429. a->nla_type = attrtype;
  430. a->nla_len = nla_attr_size(len);
  431. if (data)
  432. memcpy(nla_data(a), data, len);
  433. memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
  434. return 0;
  435. }
  436. static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
  437. {
  438. int used = (*sfa)->actions_len;
  439. int err;
  440. err = add_action(sfa, attrtype, NULL, 0);
  441. if (err)
  442. return err;
  443. return used;
  444. }
  445. static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
  446. {
  447. struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
  448. a->nla_len = sfa->actions_len - st_offset;
  449. }
  450. static int validate_and_copy_actions(const struct nlattr *attr,
  451. const struct sw_flow_key *key, int depth,
  452. struct sw_flow_actions **sfa);
  453. static int validate_and_copy_sample(const struct nlattr *attr,
  454. const struct sw_flow_key *key, int depth,
  455. struct sw_flow_actions **sfa)
  456. {
  457. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  458. const struct nlattr *probability, *actions;
  459. const struct nlattr *a;
  460. int rem, start, err, st_acts;
  461. memset(attrs, 0, sizeof(attrs));
  462. nla_for_each_nested(a, attr, rem) {
  463. int type = nla_type(a);
  464. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  465. return -EINVAL;
  466. attrs[type] = a;
  467. }
  468. if (rem)
  469. return -EINVAL;
  470. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  471. if (!probability || nla_len(probability) != sizeof(u32))
  472. return -EINVAL;
  473. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  474. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  475. return -EINVAL;
  476. /* validation done, copy sample action. */
  477. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
  478. if (start < 0)
  479. return start;
  480. err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
  481. if (err)
  482. return err;
  483. st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
  484. if (st_acts < 0)
  485. return st_acts;
  486. err = validate_and_copy_actions(actions, key, depth + 1, sfa);
  487. if (err)
  488. return err;
  489. add_nested_action_end(*sfa, st_acts);
  490. add_nested_action_end(*sfa, start);
  491. return 0;
  492. }
  493. static int validate_tp_port(const struct sw_flow_key *flow_key)
  494. {
  495. if (flow_key->eth.type == htons(ETH_P_IP)) {
  496. if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
  497. return 0;
  498. } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
  499. if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
  500. return 0;
  501. }
  502. return -EINVAL;
  503. }
  504. static int validate_and_copy_set_tun(const struct nlattr *attr,
  505. struct sw_flow_actions **sfa)
  506. {
  507. struct ovs_key_ipv4_tunnel tun_key;
  508. int err, start;
  509. err = ovs_ipv4_tun_from_nlattr(nla_data(attr), &tun_key);
  510. if (err)
  511. return err;
  512. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
  513. if (start < 0)
  514. return start;
  515. err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &tun_key, sizeof(tun_key));
  516. add_nested_action_end(*sfa, start);
  517. return err;
  518. }
  519. static int validate_set(const struct nlattr *a,
  520. const struct sw_flow_key *flow_key,
  521. struct sw_flow_actions **sfa,
  522. bool *set_tun)
  523. {
  524. const struct nlattr *ovs_key = nla_data(a);
  525. int key_type = nla_type(ovs_key);
  526. /* There can be only one key in a action */
  527. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  528. return -EINVAL;
  529. if (key_type > OVS_KEY_ATTR_MAX ||
  530. (ovs_key_lens[key_type] != nla_len(ovs_key) &&
  531. ovs_key_lens[key_type] != -1))
  532. return -EINVAL;
  533. switch (key_type) {
  534. const struct ovs_key_ipv4 *ipv4_key;
  535. const struct ovs_key_ipv6 *ipv6_key;
  536. int err;
  537. case OVS_KEY_ATTR_PRIORITY:
  538. case OVS_KEY_ATTR_SKB_MARK:
  539. case OVS_KEY_ATTR_ETHERNET:
  540. break;
  541. case OVS_KEY_ATTR_TUNNEL:
  542. *set_tun = true;
  543. err = validate_and_copy_set_tun(a, sfa);
  544. if (err)
  545. return err;
  546. break;
  547. case OVS_KEY_ATTR_IPV4:
  548. if (flow_key->eth.type != htons(ETH_P_IP))
  549. return -EINVAL;
  550. if (!flow_key->ip.proto)
  551. return -EINVAL;
  552. ipv4_key = nla_data(ovs_key);
  553. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  554. return -EINVAL;
  555. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  556. return -EINVAL;
  557. break;
  558. case OVS_KEY_ATTR_IPV6:
  559. if (flow_key->eth.type != htons(ETH_P_IPV6))
  560. return -EINVAL;
  561. if (!flow_key->ip.proto)
  562. return -EINVAL;
  563. ipv6_key = nla_data(ovs_key);
  564. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  565. return -EINVAL;
  566. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  567. return -EINVAL;
  568. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  569. return -EINVAL;
  570. break;
  571. case OVS_KEY_ATTR_TCP:
  572. if (flow_key->ip.proto != IPPROTO_TCP)
  573. return -EINVAL;
  574. return validate_tp_port(flow_key);
  575. case OVS_KEY_ATTR_UDP:
  576. if (flow_key->ip.proto != IPPROTO_UDP)
  577. return -EINVAL;
  578. return validate_tp_port(flow_key);
  579. default:
  580. return -EINVAL;
  581. }
  582. return 0;
  583. }
  584. static int validate_userspace(const struct nlattr *attr)
  585. {
  586. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  587. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  588. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
  589. };
  590. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  591. int error;
  592. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  593. attr, userspace_policy);
  594. if (error)
  595. return error;
  596. if (!a[OVS_USERSPACE_ATTR_PID] ||
  597. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  598. return -EINVAL;
  599. return 0;
  600. }
  601. static int copy_action(const struct nlattr *from,
  602. struct sw_flow_actions **sfa)
  603. {
  604. int totlen = NLA_ALIGN(from->nla_len);
  605. struct nlattr *to;
  606. to = reserve_sfa_size(sfa, from->nla_len);
  607. if (IS_ERR(to))
  608. return PTR_ERR(to);
  609. memcpy(to, from, totlen);
  610. return 0;
  611. }
  612. static int validate_and_copy_actions(const struct nlattr *attr,
  613. const struct sw_flow_key *key,
  614. int depth,
  615. struct sw_flow_actions **sfa)
  616. {
  617. const struct nlattr *a;
  618. int rem, err;
  619. if (depth >= SAMPLE_ACTION_DEPTH)
  620. return -EOVERFLOW;
  621. nla_for_each_nested(a, attr, rem) {
  622. /* Expected argument lengths, (u32)-1 for variable length. */
  623. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  624. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  625. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  626. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  627. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  628. [OVS_ACTION_ATTR_SET] = (u32)-1,
  629. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
  630. };
  631. const struct ovs_action_push_vlan *vlan;
  632. int type = nla_type(a);
  633. bool skip_copy;
  634. if (type > OVS_ACTION_ATTR_MAX ||
  635. (action_lens[type] != nla_len(a) &&
  636. action_lens[type] != (u32)-1))
  637. return -EINVAL;
  638. skip_copy = false;
  639. switch (type) {
  640. case OVS_ACTION_ATTR_UNSPEC:
  641. return -EINVAL;
  642. case OVS_ACTION_ATTR_USERSPACE:
  643. err = validate_userspace(a);
  644. if (err)
  645. return err;
  646. break;
  647. case OVS_ACTION_ATTR_OUTPUT:
  648. if (nla_get_u32(a) >= DP_MAX_PORTS)
  649. return -EINVAL;
  650. break;
  651. case OVS_ACTION_ATTR_POP_VLAN:
  652. break;
  653. case OVS_ACTION_ATTR_PUSH_VLAN:
  654. vlan = nla_data(a);
  655. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  656. return -EINVAL;
  657. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  658. return -EINVAL;
  659. break;
  660. case OVS_ACTION_ATTR_SET:
  661. err = validate_set(a, key, sfa, &skip_copy);
  662. if (err)
  663. return err;
  664. break;
  665. case OVS_ACTION_ATTR_SAMPLE:
  666. err = validate_and_copy_sample(a, key, depth, sfa);
  667. if (err)
  668. return err;
  669. skip_copy = true;
  670. break;
  671. default:
  672. return -EINVAL;
  673. }
  674. if (!skip_copy) {
  675. err = copy_action(a, sfa);
  676. if (err)
  677. return err;
  678. }
  679. }
  680. if (rem > 0)
  681. return -EINVAL;
  682. return 0;
  683. }
  684. static void clear_stats(struct sw_flow *flow)
  685. {
  686. flow->used = 0;
  687. flow->tcp_flags = 0;
  688. flow->packet_count = 0;
  689. flow->byte_count = 0;
  690. }
  691. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  692. {
  693. struct ovs_header *ovs_header = info->userhdr;
  694. struct nlattr **a = info->attrs;
  695. struct sw_flow_actions *acts;
  696. struct sk_buff *packet;
  697. struct sw_flow *flow;
  698. struct datapath *dp;
  699. struct ethhdr *eth;
  700. int len;
  701. int err;
  702. int key_len;
  703. err = -EINVAL;
  704. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  705. !a[OVS_PACKET_ATTR_ACTIONS])
  706. goto err;
  707. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  708. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  709. err = -ENOMEM;
  710. if (!packet)
  711. goto err;
  712. skb_reserve(packet, NET_IP_ALIGN);
  713. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  714. skb_reset_mac_header(packet);
  715. eth = eth_hdr(packet);
  716. /* Normally, setting the skb 'protocol' field would be handled by a
  717. * call to eth_type_trans(), but it assumes there's a sending
  718. * device, which we may not have. */
  719. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  720. packet->protocol = eth->h_proto;
  721. else
  722. packet->protocol = htons(ETH_P_802_2);
  723. /* Build an sw_flow for sending this packet. */
  724. flow = ovs_flow_alloc();
  725. err = PTR_ERR(flow);
  726. if (IS_ERR(flow))
  727. goto err_kfree_skb;
  728. err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
  729. if (err)
  730. goto err_flow_free;
  731. err = ovs_flow_metadata_from_nlattrs(flow, key_len, a[OVS_PACKET_ATTR_KEY]);
  732. if (err)
  733. goto err_flow_free;
  734. acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
  735. err = PTR_ERR(acts);
  736. if (IS_ERR(acts))
  737. goto err_flow_free;
  738. err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
  739. rcu_assign_pointer(flow->sf_acts, acts);
  740. if (err)
  741. goto err_flow_free;
  742. OVS_CB(packet)->flow = flow;
  743. packet->priority = flow->key.phy.priority;
  744. packet->mark = flow->key.phy.skb_mark;
  745. rcu_read_lock();
  746. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  747. err = -ENODEV;
  748. if (!dp)
  749. goto err_unlock;
  750. local_bh_disable();
  751. err = ovs_execute_actions(dp, packet);
  752. local_bh_enable();
  753. rcu_read_unlock();
  754. ovs_flow_free(flow);
  755. return err;
  756. err_unlock:
  757. rcu_read_unlock();
  758. err_flow_free:
  759. ovs_flow_free(flow);
  760. err_kfree_skb:
  761. kfree_skb(packet);
  762. err:
  763. return err;
  764. }
  765. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  766. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  767. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  768. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  769. };
  770. static struct genl_ops dp_packet_genl_ops[] = {
  771. { .cmd = OVS_PACKET_CMD_EXECUTE,
  772. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  773. .policy = packet_policy,
  774. .doit = ovs_packet_cmd_execute
  775. }
  776. };
  777. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
  778. {
  779. int i;
  780. struct flow_table *table = ovsl_dereference(dp->table);
  781. stats->n_flows = ovs_flow_tbl_count(table);
  782. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  783. for_each_possible_cpu(i) {
  784. const struct dp_stats_percpu *percpu_stats;
  785. struct dp_stats_percpu local_stats;
  786. unsigned int start;
  787. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  788. do {
  789. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  790. local_stats = *percpu_stats;
  791. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  792. stats->n_hit += local_stats.n_hit;
  793. stats->n_missed += local_stats.n_missed;
  794. stats->n_lost += local_stats.n_lost;
  795. }
  796. }
  797. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  798. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  799. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  800. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  801. };
  802. static struct genl_family dp_flow_genl_family = {
  803. .id = GENL_ID_GENERATE,
  804. .hdrsize = sizeof(struct ovs_header),
  805. .name = OVS_FLOW_FAMILY,
  806. .version = OVS_FLOW_VERSION,
  807. .maxattr = OVS_FLOW_ATTR_MAX,
  808. .netnsok = true,
  809. .parallel_ops = true,
  810. };
  811. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  812. .name = OVS_FLOW_MCGROUP
  813. };
  814. static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
  815. static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
  816. {
  817. const struct nlattr *a;
  818. struct nlattr *start;
  819. int err = 0, rem;
  820. start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
  821. if (!start)
  822. return -EMSGSIZE;
  823. nla_for_each_nested(a, attr, rem) {
  824. int type = nla_type(a);
  825. struct nlattr *st_sample;
  826. switch (type) {
  827. case OVS_SAMPLE_ATTR_PROBABILITY:
  828. if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
  829. return -EMSGSIZE;
  830. break;
  831. case OVS_SAMPLE_ATTR_ACTIONS:
  832. st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
  833. if (!st_sample)
  834. return -EMSGSIZE;
  835. err = actions_to_attr(nla_data(a), nla_len(a), skb);
  836. if (err)
  837. return err;
  838. nla_nest_end(skb, st_sample);
  839. break;
  840. }
  841. }
  842. nla_nest_end(skb, start);
  843. return err;
  844. }
  845. static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
  846. {
  847. const struct nlattr *ovs_key = nla_data(a);
  848. int key_type = nla_type(ovs_key);
  849. struct nlattr *start;
  850. int err;
  851. switch (key_type) {
  852. case OVS_KEY_ATTR_IPV4_TUNNEL:
  853. start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  854. if (!start)
  855. return -EMSGSIZE;
  856. err = ovs_ipv4_tun_to_nlattr(skb, nla_data(ovs_key));
  857. if (err)
  858. return err;
  859. nla_nest_end(skb, start);
  860. break;
  861. default:
  862. if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
  863. return -EMSGSIZE;
  864. break;
  865. }
  866. return 0;
  867. }
  868. static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
  869. {
  870. const struct nlattr *a;
  871. int rem, err;
  872. nla_for_each_attr(a, attr, len, rem) {
  873. int type = nla_type(a);
  874. switch (type) {
  875. case OVS_ACTION_ATTR_SET:
  876. err = set_action_to_attr(a, skb);
  877. if (err)
  878. return err;
  879. break;
  880. case OVS_ACTION_ATTR_SAMPLE:
  881. err = sample_action_to_attr(a, skb);
  882. if (err)
  883. return err;
  884. break;
  885. default:
  886. if (nla_put(skb, type, nla_len(a), nla_data(a)))
  887. return -EMSGSIZE;
  888. break;
  889. }
  890. }
  891. return 0;
  892. }
  893. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  894. {
  895. return NLMSG_ALIGN(sizeof(struct ovs_header))
  896. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  897. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  898. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  899. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  900. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  901. }
  902. /* Called with ovs_mutex. */
  903. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  904. struct sk_buff *skb, u32 portid,
  905. u32 seq, u32 flags, u8 cmd)
  906. {
  907. const int skb_orig_len = skb->len;
  908. const struct sw_flow_actions *sf_acts;
  909. struct nlattr *start;
  910. struct ovs_flow_stats stats;
  911. struct ovs_header *ovs_header;
  912. struct nlattr *nla;
  913. unsigned long used;
  914. u8 tcp_flags;
  915. int err;
  916. sf_acts = ovsl_dereference(flow->sf_acts);
  917. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  918. if (!ovs_header)
  919. return -EMSGSIZE;
  920. ovs_header->dp_ifindex = get_dpifindex(dp);
  921. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  922. if (!nla)
  923. goto nla_put_failure;
  924. err = ovs_flow_to_nlattrs(&flow->key, skb);
  925. if (err)
  926. goto error;
  927. nla_nest_end(skb, nla);
  928. spin_lock_bh(&flow->lock);
  929. used = flow->used;
  930. stats.n_packets = flow->packet_count;
  931. stats.n_bytes = flow->byte_count;
  932. tcp_flags = flow->tcp_flags;
  933. spin_unlock_bh(&flow->lock);
  934. if (used &&
  935. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  936. goto nla_put_failure;
  937. if (stats.n_packets &&
  938. nla_put(skb, OVS_FLOW_ATTR_STATS,
  939. sizeof(struct ovs_flow_stats), &stats))
  940. goto nla_put_failure;
  941. if (tcp_flags &&
  942. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
  943. goto nla_put_failure;
  944. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  945. * this is the first flow to be dumped into 'skb'. This is unusual for
  946. * Netlink but individual action lists can be longer than
  947. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  948. * The userspace caller can always fetch the actions separately if it
  949. * really wants them. (Most userspace callers in fact don't care.)
  950. *
  951. * This can only fail for dump operations because the skb is always
  952. * properly sized for single flows.
  953. */
  954. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  955. if (start) {
  956. err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
  957. if (!err)
  958. nla_nest_end(skb, start);
  959. else {
  960. if (skb_orig_len)
  961. goto error;
  962. nla_nest_cancel(skb, start);
  963. }
  964. } else if (skb_orig_len)
  965. goto nla_put_failure;
  966. return genlmsg_end(skb, ovs_header);
  967. nla_put_failure:
  968. err = -EMSGSIZE;
  969. error:
  970. genlmsg_cancel(skb, ovs_header);
  971. return err;
  972. }
  973. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  974. {
  975. const struct sw_flow_actions *sf_acts;
  976. sf_acts = ovsl_dereference(flow->sf_acts);
  977. return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
  978. }
  979. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  980. struct datapath *dp,
  981. u32 portid, u32 seq, u8 cmd)
  982. {
  983. struct sk_buff *skb;
  984. int retval;
  985. skb = ovs_flow_cmd_alloc_info(flow);
  986. if (!skb)
  987. return ERR_PTR(-ENOMEM);
  988. retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
  989. BUG_ON(retval < 0);
  990. return skb;
  991. }
  992. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  993. {
  994. struct nlattr **a = info->attrs;
  995. struct ovs_header *ovs_header = info->userhdr;
  996. struct sw_flow_key key;
  997. struct sw_flow *flow;
  998. struct sk_buff *reply;
  999. struct datapath *dp;
  1000. struct flow_table *table;
  1001. struct sw_flow_actions *acts = NULL;
  1002. int error;
  1003. int key_len;
  1004. /* Extract key. */
  1005. error = -EINVAL;
  1006. if (!a[OVS_FLOW_ATTR_KEY])
  1007. goto error;
  1008. error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  1009. if (error)
  1010. goto error;
  1011. /* Validate actions. */
  1012. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  1013. acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
  1014. error = PTR_ERR(acts);
  1015. if (IS_ERR(acts))
  1016. goto error;
  1017. error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0, &acts);
  1018. if (error)
  1019. goto err_kfree;
  1020. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  1021. error = -EINVAL;
  1022. goto error;
  1023. }
  1024. ovs_lock();
  1025. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1026. error = -ENODEV;
  1027. if (!dp)
  1028. goto err_unlock_ovs;
  1029. table = ovsl_dereference(dp->table);
  1030. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  1031. if (!flow) {
  1032. /* Bail out if we're not allowed to create a new flow. */
  1033. error = -ENOENT;
  1034. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  1035. goto err_unlock_ovs;
  1036. /* Expand table, if necessary, to make room. */
  1037. if (ovs_flow_tbl_need_to_expand(table)) {
  1038. struct flow_table *new_table;
  1039. new_table = ovs_flow_tbl_expand(table);
  1040. if (!IS_ERR(new_table)) {
  1041. rcu_assign_pointer(dp->table, new_table);
  1042. ovs_flow_tbl_deferred_destroy(table);
  1043. table = ovsl_dereference(dp->table);
  1044. }
  1045. }
  1046. /* Allocate flow. */
  1047. flow = ovs_flow_alloc();
  1048. if (IS_ERR(flow)) {
  1049. error = PTR_ERR(flow);
  1050. goto err_unlock_ovs;
  1051. }
  1052. clear_stats(flow);
  1053. rcu_assign_pointer(flow->sf_acts, acts);
  1054. /* Put flow in bucket. */
  1055. ovs_flow_tbl_insert(table, flow, &key, key_len);
  1056. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  1057. info->snd_seq,
  1058. OVS_FLOW_CMD_NEW);
  1059. } else {
  1060. /* We found a matching flow. */
  1061. struct sw_flow_actions *old_acts;
  1062. /* Bail out if we're not allowed to modify an existing flow.
  1063. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  1064. * because Generic Netlink treats the latter as a dump
  1065. * request. We also accept NLM_F_EXCL in case that bug ever
  1066. * gets fixed.
  1067. */
  1068. error = -EEXIST;
  1069. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  1070. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  1071. goto err_unlock_ovs;
  1072. /* Update actions. */
  1073. old_acts = ovsl_dereference(flow->sf_acts);
  1074. rcu_assign_pointer(flow->sf_acts, acts);
  1075. ovs_flow_deferred_free_acts(old_acts);
  1076. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  1077. info->snd_seq, OVS_FLOW_CMD_NEW);
  1078. /* Clear stats. */
  1079. if (a[OVS_FLOW_ATTR_CLEAR]) {
  1080. spin_lock_bh(&flow->lock);
  1081. clear_stats(flow);
  1082. spin_unlock_bh(&flow->lock);
  1083. }
  1084. }
  1085. ovs_unlock();
  1086. if (!IS_ERR(reply))
  1087. ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
  1088. else
  1089. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1090. ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
  1091. return 0;
  1092. err_unlock_ovs:
  1093. ovs_unlock();
  1094. err_kfree:
  1095. kfree(acts);
  1096. error:
  1097. return error;
  1098. }
  1099. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1100. {
  1101. struct nlattr **a = info->attrs;
  1102. struct ovs_header *ovs_header = info->userhdr;
  1103. struct sw_flow_key key;
  1104. struct sk_buff *reply;
  1105. struct sw_flow *flow;
  1106. struct datapath *dp;
  1107. struct flow_table *table;
  1108. int err;
  1109. int key_len;
  1110. if (!a[OVS_FLOW_ATTR_KEY])
  1111. return -EINVAL;
  1112. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  1113. if (err)
  1114. return err;
  1115. ovs_lock();
  1116. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1117. if (!dp) {
  1118. err = -ENODEV;
  1119. goto unlock;
  1120. }
  1121. table = ovsl_dereference(dp->table);
  1122. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  1123. if (!flow) {
  1124. err = -ENOENT;
  1125. goto unlock;
  1126. }
  1127. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  1128. info->snd_seq, OVS_FLOW_CMD_NEW);
  1129. if (IS_ERR(reply)) {
  1130. err = PTR_ERR(reply);
  1131. goto unlock;
  1132. }
  1133. ovs_unlock();
  1134. return genlmsg_reply(reply, info);
  1135. unlock:
  1136. ovs_unlock();
  1137. return err;
  1138. }
  1139. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1140. {
  1141. struct nlattr **a = info->attrs;
  1142. struct ovs_header *ovs_header = info->userhdr;
  1143. struct sw_flow_key key;
  1144. struct sk_buff *reply;
  1145. struct sw_flow *flow;
  1146. struct datapath *dp;
  1147. struct flow_table *table;
  1148. int err;
  1149. int key_len;
  1150. ovs_lock();
  1151. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1152. if (!dp) {
  1153. err = -ENODEV;
  1154. goto unlock;
  1155. }
  1156. if (!a[OVS_FLOW_ATTR_KEY]) {
  1157. err = flush_flows(dp);
  1158. goto unlock;
  1159. }
  1160. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  1161. if (err)
  1162. goto unlock;
  1163. table = ovsl_dereference(dp->table);
  1164. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  1165. if (!flow) {
  1166. err = -ENOENT;
  1167. goto unlock;
  1168. }
  1169. reply = ovs_flow_cmd_alloc_info(flow);
  1170. if (!reply) {
  1171. err = -ENOMEM;
  1172. goto unlock;
  1173. }
  1174. ovs_flow_tbl_remove(table, flow);
  1175. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
  1176. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  1177. BUG_ON(err < 0);
  1178. ovs_flow_deferred_free(flow);
  1179. ovs_unlock();
  1180. ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
  1181. return 0;
  1182. unlock:
  1183. ovs_unlock();
  1184. return err;
  1185. }
  1186. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1187. {
  1188. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1189. struct datapath *dp;
  1190. struct flow_table *table;
  1191. ovs_lock();
  1192. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1193. if (!dp) {
  1194. ovs_unlock();
  1195. return -ENODEV;
  1196. }
  1197. table = ovsl_dereference(dp->table);
  1198. for (;;) {
  1199. struct sw_flow *flow;
  1200. u32 bucket, obj;
  1201. bucket = cb->args[0];
  1202. obj = cb->args[1];
  1203. flow = ovs_flow_tbl_next(table, &bucket, &obj);
  1204. if (!flow)
  1205. break;
  1206. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  1207. NETLINK_CB(cb->skb).portid,
  1208. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1209. OVS_FLOW_CMD_NEW) < 0)
  1210. break;
  1211. cb->args[0] = bucket;
  1212. cb->args[1] = obj;
  1213. }
  1214. ovs_unlock();
  1215. return skb->len;
  1216. }
  1217. static struct genl_ops dp_flow_genl_ops[] = {
  1218. { .cmd = OVS_FLOW_CMD_NEW,
  1219. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1220. .policy = flow_policy,
  1221. .doit = ovs_flow_cmd_new_or_set
  1222. },
  1223. { .cmd = OVS_FLOW_CMD_DEL,
  1224. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1225. .policy = flow_policy,
  1226. .doit = ovs_flow_cmd_del
  1227. },
  1228. { .cmd = OVS_FLOW_CMD_GET,
  1229. .flags = 0, /* OK for unprivileged users. */
  1230. .policy = flow_policy,
  1231. .doit = ovs_flow_cmd_get,
  1232. .dumpit = ovs_flow_cmd_dump
  1233. },
  1234. { .cmd = OVS_FLOW_CMD_SET,
  1235. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1236. .policy = flow_policy,
  1237. .doit = ovs_flow_cmd_new_or_set,
  1238. },
  1239. };
  1240. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1241. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1242. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1243. };
  1244. static struct genl_family dp_datapath_genl_family = {
  1245. .id = GENL_ID_GENERATE,
  1246. .hdrsize = sizeof(struct ovs_header),
  1247. .name = OVS_DATAPATH_FAMILY,
  1248. .version = OVS_DATAPATH_VERSION,
  1249. .maxattr = OVS_DP_ATTR_MAX,
  1250. .netnsok = true,
  1251. .parallel_ops = true,
  1252. };
  1253. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  1254. .name = OVS_DATAPATH_MCGROUP
  1255. };
  1256. static size_t ovs_dp_cmd_msg_size(void)
  1257. {
  1258. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1259. msgsize += nla_total_size(IFNAMSIZ);
  1260. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  1261. return msgsize;
  1262. }
  1263. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1264. u32 portid, u32 seq, u32 flags, u8 cmd)
  1265. {
  1266. struct ovs_header *ovs_header;
  1267. struct ovs_dp_stats dp_stats;
  1268. int err;
  1269. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1270. flags, cmd);
  1271. if (!ovs_header)
  1272. goto error;
  1273. ovs_header->dp_ifindex = get_dpifindex(dp);
  1274. rcu_read_lock();
  1275. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1276. rcu_read_unlock();
  1277. if (err)
  1278. goto nla_put_failure;
  1279. get_dp_stats(dp, &dp_stats);
  1280. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
  1281. goto nla_put_failure;
  1282. return genlmsg_end(skb, ovs_header);
  1283. nla_put_failure:
  1284. genlmsg_cancel(skb, ovs_header);
  1285. error:
  1286. return -EMSGSIZE;
  1287. }
  1288. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
  1289. u32 seq, u8 cmd)
  1290. {
  1291. struct sk_buff *skb;
  1292. int retval;
  1293. skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
  1294. if (!skb)
  1295. return ERR_PTR(-ENOMEM);
  1296. retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
  1297. if (retval < 0) {
  1298. kfree_skb(skb);
  1299. return ERR_PTR(retval);
  1300. }
  1301. return skb;
  1302. }
  1303. /* Called with ovs_mutex. */
  1304. static struct datapath *lookup_datapath(struct net *net,
  1305. struct ovs_header *ovs_header,
  1306. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1307. {
  1308. struct datapath *dp;
  1309. if (!a[OVS_DP_ATTR_NAME])
  1310. dp = get_dp(net, ovs_header->dp_ifindex);
  1311. else {
  1312. struct vport *vport;
  1313. rcu_read_lock();
  1314. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1315. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1316. rcu_read_unlock();
  1317. }
  1318. return dp ? dp : ERR_PTR(-ENODEV);
  1319. }
  1320. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1321. {
  1322. struct nlattr **a = info->attrs;
  1323. struct vport_parms parms;
  1324. struct sk_buff *reply;
  1325. struct datapath *dp;
  1326. struct vport *vport;
  1327. struct ovs_net *ovs_net;
  1328. int err, i;
  1329. err = -EINVAL;
  1330. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1331. goto err;
  1332. ovs_lock();
  1333. err = -ENOMEM;
  1334. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1335. if (dp == NULL)
  1336. goto err_unlock_ovs;
  1337. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1338. /* Allocate table. */
  1339. err = -ENOMEM;
  1340. rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
  1341. if (!dp->table)
  1342. goto err_free_dp;
  1343. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1344. if (!dp->stats_percpu) {
  1345. err = -ENOMEM;
  1346. goto err_destroy_table;
  1347. }
  1348. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1349. GFP_KERNEL);
  1350. if (!dp->ports) {
  1351. err = -ENOMEM;
  1352. goto err_destroy_percpu;
  1353. }
  1354. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1355. INIT_HLIST_HEAD(&dp->ports[i]);
  1356. /* Set up our datapath device. */
  1357. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1358. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1359. parms.options = NULL;
  1360. parms.dp = dp;
  1361. parms.port_no = OVSP_LOCAL;
  1362. parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1363. vport = new_vport(&parms);
  1364. if (IS_ERR(vport)) {
  1365. err = PTR_ERR(vport);
  1366. if (err == -EBUSY)
  1367. err = -EEXIST;
  1368. goto err_destroy_ports_array;
  1369. }
  1370. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1371. info->snd_seq, OVS_DP_CMD_NEW);
  1372. err = PTR_ERR(reply);
  1373. if (IS_ERR(reply))
  1374. goto err_destroy_local_port;
  1375. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1376. list_add_tail(&dp->list_node, &ovs_net->dps);
  1377. ovs_unlock();
  1378. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1379. return 0;
  1380. err_destroy_local_port:
  1381. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1382. err_destroy_ports_array:
  1383. kfree(dp->ports);
  1384. err_destroy_percpu:
  1385. free_percpu(dp->stats_percpu);
  1386. err_destroy_table:
  1387. ovs_flow_tbl_destroy(ovsl_dereference(dp->table));
  1388. err_free_dp:
  1389. release_net(ovs_dp_get_net(dp));
  1390. kfree(dp);
  1391. err_unlock_ovs:
  1392. ovs_unlock();
  1393. err:
  1394. return err;
  1395. }
  1396. /* Called with ovs_mutex. */
  1397. static void __dp_destroy(struct datapath *dp)
  1398. {
  1399. int i;
  1400. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1401. struct vport *vport;
  1402. struct hlist_node *n;
  1403. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1404. if (vport->port_no != OVSP_LOCAL)
  1405. ovs_dp_detach_port(vport);
  1406. }
  1407. list_del(&dp->list_node);
  1408. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1409. * all port in datapath are destroyed first before freeing datapath.
  1410. */
  1411. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1412. call_rcu(&dp->rcu, destroy_dp_rcu);
  1413. }
  1414. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1415. {
  1416. struct sk_buff *reply;
  1417. struct datapath *dp;
  1418. int err;
  1419. ovs_lock();
  1420. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1421. err = PTR_ERR(dp);
  1422. if (IS_ERR(dp))
  1423. goto unlock;
  1424. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1425. info->snd_seq, OVS_DP_CMD_DEL);
  1426. err = PTR_ERR(reply);
  1427. if (IS_ERR(reply))
  1428. goto unlock;
  1429. __dp_destroy(dp);
  1430. ovs_unlock();
  1431. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1432. return 0;
  1433. unlock:
  1434. ovs_unlock();
  1435. return err;
  1436. }
  1437. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1438. {
  1439. struct sk_buff *reply;
  1440. struct datapath *dp;
  1441. int err;
  1442. ovs_lock();
  1443. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1444. err = PTR_ERR(dp);
  1445. if (IS_ERR(dp))
  1446. goto unlock;
  1447. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1448. info->snd_seq, OVS_DP_CMD_NEW);
  1449. if (IS_ERR(reply)) {
  1450. err = PTR_ERR(reply);
  1451. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1452. ovs_dp_datapath_multicast_group.id, err);
  1453. err = 0;
  1454. goto unlock;
  1455. }
  1456. ovs_unlock();
  1457. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1458. return 0;
  1459. unlock:
  1460. ovs_unlock();
  1461. return err;
  1462. }
  1463. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1464. {
  1465. struct sk_buff *reply;
  1466. struct datapath *dp;
  1467. int err;
  1468. ovs_lock();
  1469. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1470. if (IS_ERR(dp)) {
  1471. err = PTR_ERR(dp);
  1472. goto unlock;
  1473. }
  1474. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1475. info->snd_seq, OVS_DP_CMD_NEW);
  1476. if (IS_ERR(reply)) {
  1477. err = PTR_ERR(reply);
  1478. goto unlock;
  1479. }
  1480. ovs_unlock();
  1481. return genlmsg_reply(reply, info);
  1482. unlock:
  1483. ovs_unlock();
  1484. return err;
  1485. }
  1486. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1487. {
  1488. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1489. struct datapath *dp;
  1490. int skip = cb->args[0];
  1491. int i = 0;
  1492. ovs_lock();
  1493. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1494. if (i >= skip &&
  1495. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1496. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1497. OVS_DP_CMD_NEW) < 0)
  1498. break;
  1499. i++;
  1500. }
  1501. ovs_unlock();
  1502. cb->args[0] = i;
  1503. return skb->len;
  1504. }
  1505. static struct genl_ops dp_datapath_genl_ops[] = {
  1506. { .cmd = OVS_DP_CMD_NEW,
  1507. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1508. .policy = datapath_policy,
  1509. .doit = ovs_dp_cmd_new
  1510. },
  1511. { .cmd = OVS_DP_CMD_DEL,
  1512. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1513. .policy = datapath_policy,
  1514. .doit = ovs_dp_cmd_del
  1515. },
  1516. { .cmd = OVS_DP_CMD_GET,
  1517. .flags = 0, /* OK for unprivileged users. */
  1518. .policy = datapath_policy,
  1519. .doit = ovs_dp_cmd_get,
  1520. .dumpit = ovs_dp_cmd_dump
  1521. },
  1522. { .cmd = OVS_DP_CMD_SET,
  1523. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1524. .policy = datapath_policy,
  1525. .doit = ovs_dp_cmd_set,
  1526. },
  1527. };
  1528. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1529. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1530. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1531. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1532. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1533. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1534. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1535. };
  1536. static struct genl_family dp_vport_genl_family = {
  1537. .id = GENL_ID_GENERATE,
  1538. .hdrsize = sizeof(struct ovs_header),
  1539. .name = OVS_VPORT_FAMILY,
  1540. .version = OVS_VPORT_VERSION,
  1541. .maxattr = OVS_VPORT_ATTR_MAX,
  1542. .netnsok = true,
  1543. .parallel_ops = true,
  1544. };
  1545. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1546. .name = OVS_VPORT_MCGROUP
  1547. };
  1548. /* Called with ovs_mutex or RCU read lock. */
  1549. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1550. u32 portid, u32 seq, u32 flags, u8 cmd)
  1551. {
  1552. struct ovs_header *ovs_header;
  1553. struct ovs_vport_stats vport_stats;
  1554. int err;
  1555. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1556. flags, cmd);
  1557. if (!ovs_header)
  1558. return -EMSGSIZE;
  1559. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1560. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1561. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1562. nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
  1563. nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
  1564. goto nla_put_failure;
  1565. ovs_vport_get_stats(vport, &vport_stats);
  1566. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1567. &vport_stats))
  1568. goto nla_put_failure;
  1569. err = ovs_vport_get_options(vport, skb);
  1570. if (err == -EMSGSIZE)
  1571. goto error;
  1572. return genlmsg_end(skb, ovs_header);
  1573. nla_put_failure:
  1574. err = -EMSGSIZE;
  1575. error:
  1576. genlmsg_cancel(skb, ovs_header);
  1577. return err;
  1578. }
  1579. /* Called with ovs_mutex or RCU read lock. */
  1580. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1581. u32 seq, u8 cmd)
  1582. {
  1583. struct sk_buff *skb;
  1584. int retval;
  1585. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1586. if (!skb)
  1587. return ERR_PTR(-ENOMEM);
  1588. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1589. BUG_ON(retval < 0);
  1590. return skb;
  1591. }
  1592. /* Called with ovs_mutex or RCU read lock. */
  1593. static struct vport *lookup_vport(struct net *net,
  1594. struct ovs_header *ovs_header,
  1595. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1596. {
  1597. struct datapath *dp;
  1598. struct vport *vport;
  1599. if (a[OVS_VPORT_ATTR_NAME]) {
  1600. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1601. if (!vport)
  1602. return ERR_PTR(-ENODEV);
  1603. if (ovs_header->dp_ifindex &&
  1604. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1605. return ERR_PTR(-ENODEV);
  1606. return vport;
  1607. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1608. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1609. if (port_no >= DP_MAX_PORTS)
  1610. return ERR_PTR(-EFBIG);
  1611. dp = get_dp(net, ovs_header->dp_ifindex);
  1612. if (!dp)
  1613. return ERR_PTR(-ENODEV);
  1614. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1615. if (!vport)
  1616. return ERR_PTR(-ENODEV);
  1617. return vport;
  1618. } else
  1619. return ERR_PTR(-EINVAL);
  1620. }
  1621. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1622. {
  1623. struct nlattr **a = info->attrs;
  1624. struct ovs_header *ovs_header = info->userhdr;
  1625. struct vport_parms parms;
  1626. struct sk_buff *reply;
  1627. struct vport *vport;
  1628. struct datapath *dp;
  1629. u32 port_no;
  1630. int err;
  1631. err = -EINVAL;
  1632. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1633. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1634. goto exit;
  1635. ovs_lock();
  1636. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1637. err = -ENODEV;
  1638. if (!dp)
  1639. goto exit_unlock;
  1640. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1641. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1642. err = -EFBIG;
  1643. if (port_no >= DP_MAX_PORTS)
  1644. goto exit_unlock;
  1645. vport = ovs_vport_ovsl(dp, port_no);
  1646. err = -EBUSY;
  1647. if (vport)
  1648. goto exit_unlock;
  1649. } else {
  1650. for (port_no = 1; ; port_no++) {
  1651. if (port_no >= DP_MAX_PORTS) {
  1652. err = -EFBIG;
  1653. goto exit_unlock;
  1654. }
  1655. vport = ovs_vport_ovsl(dp, port_no);
  1656. if (!vport)
  1657. break;
  1658. }
  1659. }
  1660. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1661. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1662. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1663. parms.dp = dp;
  1664. parms.port_no = port_no;
  1665. parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1666. vport = new_vport(&parms);
  1667. err = PTR_ERR(vport);
  1668. if (IS_ERR(vport))
  1669. goto exit_unlock;
  1670. err = 0;
  1671. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1672. OVS_VPORT_CMD_NEW);
  1673. if (IS_ERR(reply)) {
  1674. err = PTR_ERR(reply);
  1675. ovs_dp_detach_port(vport);
  1676. goto exit_unlock;
  1677. }
  1678. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1679. exit_unlock:
  1680. ovs_unlock();
  1681. exit:
  1682. return err;
  1683. }
  1684. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1685. {
  1686. struct nlattr **a = info->attrs;
  1687. struct sk_buff *reply;
  1688. struct vport *vport;
  1689. int err;
  1690. ovs_lock();
  1691. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1692. err = PTR_ERR(vport);
  1693. if (IS_ERR(vport))
  1694. goto exit_unlock;
  1695. if (a[OVS_VPORT_ATTR_TYPE] &&
  1696. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1697. err = -EINVAL;
  1698. goto exit_unlock;
  1699. }
  1700. reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1701. if (!reply) {
  1702. err = -ENOMEM;
  1703. goto exit_unlock;
  1704. }
  1705. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1706. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1707. if (err)
  1708. goto exit_free;
  1709. }
  1710. if (a[OVS_VPORT_ATTR_UPCALL_PID])
  1711. vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1712. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1713. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1714. BUG_ON(err < 0);
  1715. ovs_unlock();
  1716. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1717. return 0;
  1718. exit_free:
  1719. kfree_skb(reply);
  1720. exit_unlock:
  1721. ovs_unlock();
  1722. return err;
  1723. }
  1724. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1725. {
  1726. struct nlattr **a = info->attrs;
  1727. struct sk_buff *reply;
  1728. struct vport *vport;
  1729. int err;
  1730. ovs_lock();
  1731. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1732. err = PTR_ERR(vport);
  1733. if (IS_ERR(vport))
  1734. goto exit_unlock;
  1735. if (vport->port_no == OVSP_LOCAL) {
  1736. err = -EINVAL;
  1737. goto exit_unlock;
  1738. }
  1739. reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
  1740. info->snd_seq, OVS_VPORT_CMD_DEL);
  1741. err = PTR_ERR(reply);
  1742. if (IS_ERR(reply))
  1743. goto exit_unlock;
  1744. err = 0;
  1745. ovs_dp_detach_port(vport);
  1746. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1747. exit_unlock:
  1748. ovs_unlock();
  1749. return err;
  1750. }
  1751. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1752. {
  1753. struct nlattr **a = info->attrs;
  1754. struct ovs_header *ovs_header = info->userhdr;
  1755. struct sk_buff *reply;
  1756. struct vport *vport;
  1757. int err;
  1758. rcu_read_lock();
  1759. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1760. err = PTR_ERR(vport);
  1761. if (IS_ERR(vport))
  1762. goto exit_unlock;
  1763. reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
  1764. info->snd_seq, OVS_VPORT_CMD_NEW);
  1765. err = PTR_ERR(reply);
  1766. if (IS_ERR(reply))
  1767. goto exit_unlock;
  1768. rcu_read_unlock();
  1769. return genlmsg_reply(reply, info);
  1770. exit_unlock:
  1771. rcu_read_unlock();
  1772. return err;
  1773. }
  1774. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1775. {
  1776. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1777. struct datapath *dp;
  1778. int bucket = cb->args[0], skip = cb->args[1];
  1779. int i, j = 0;
  1780. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1781. if (!dp)
  1782. return -ENODEV;
  1783. rcu_read_lock();
  1784. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1785. struct vport *vport;
  1786. j = 0;
  1787. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1788. if (j >= skip &&
  1789. ovs_vport_cmd_fill_info(vport, skb,
  1790. NETLINK_CB(cb->skb).portid,
  1791. cb->nlh->nlmsg_seq,
  1792. NLM_F_MULTI,
  1793. OVS_VPORT_CMD_NEW) < 0)
  1794. goto out;
  1795. j++;
  1796. }
  1797. skip = 0;
  1798. }
  1799. out:
  1800. rcu_read_unlock();
  1801. cb->args[0] = i;
  1802. cb->args[1] = j;
  1803. return skb->len;
  1804. }
  1805. static struct genl_ops dp_vport_genl_ops[] = {
  1806. { .cmd = OVS_VPORT_CMD_NEW,
  1807. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1808. .policy = vport_policy,
  1809. .doit = ovs_vport_cmd_new
  1810. },
  1811. { .cmd = OVS_VPORT_CMD_DEL,
  1812. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1813. .policy = vport_policy,
  1814. .doit = ovs_vport_cmd_del
  1815. },
  1816. { .cmd = OVS_VPORT_CMD_GET,
  1817. .flags = 0, /* OK for unprivileged users. */
  1818. .policy = vport_policy,
  1819. .doit = ovs_vport_cmd_get,
  1820. .dumpit = ovs_vport_cmd_dump
  1821. },
  1822. { .cmd = OVS_VPORT_CMD_SET,
  1823. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1824. .policy = vport_policy,
  1825. .doit = ovs_vport_cmd_set,
  1826. },
  1827. };
  1828. struct genl_family_and_ops {
  1829. struct genl_family *family;
  1830. struct genl_ops *ops;
  1831. int n_ops;
  1832. struct genl_multicast_group *group;
  1833. };
  1834. static const struct genl_family_and_ops dp_genl_families[] = {
  1835. { &dp_datapath_genl_family,
  1836. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1837. &ovs_dp_datapath_multicast_group },
  1838. { &dp_vport_genl_family,
  1839. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1840. &ovs_dp_vport_multicast_group },
  1841. { &dp_flow_genl_family,
  1842. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1843. &ovs_dp_flow_multicast_group },
  1844. { &dp_packet_genl_family,
  1845. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1846. NULL },
  1847. };
  1848. static void dp_unregister_genl(int n_families)
  1849. {
  1850. int i;
  1851. for (i = 0; i < n_families; i++)
  1852. genl_unregister_family(dp_genl_families[i].family);
  1853. }
  1854. static int dp_register_genl(void)
  1855. {
  1856. int n_registered;
  1857. int err;
  1858. int i;
  1859. n_registered = 0;
  1860. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1861. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1862. err = genl_register_family_with_ops(f->family, f->ops,
  1863. f->n_ops);
  1864. if (err)
  1865. goto error;
  1866. n_registered++;
  1867. if (f->group) {
  1868. err = genl_register_mc_group(f->family, f->group);
  1869. if (err)
  1870. goto error;
  1871. }
  1872. }
  1873. return 0;
  1874. error:
  1875. dp_unregister_genl(n_registered);
  1876. return err;
  1877. }
  1878. static void rehash_flow_table(struct work_struct *work)
  1879. {
  1880. struct datapath *dp;
  1881. struct net *net;
  1882. ovs_lock();
  1883. rtnl_lock();
  1884. for_each_net(net) {
  1885. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1886. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1887. struct flow_table *old_table = ovsl_dereference(dp->table);
  1888. struct flow_table *new_table;
  1889. new_table = ovs_flow_tbl_rehash(old_table);
  1890. if (!IS_ERR(new_table)) {
  1891. rcu_assign_pointer(dp->table, new_table);
  1892. ovs_flow_tbl_deferred_destroy(old_table);
  1893. }
  1894. }
  1895. }
  1896. rtnl_unlock();
  1897. ovs_unlock();
  1898. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1899. }
  1900. static int __net_init ovs_init_net(struct net *net)
  1901. {
  1902. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1903. INIT_LIST_HEAD(&ovs_net->dps);
  1904. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1905. return 0;
  1906. }
  1907. static void __net_exit ovs_exit_net(struct net *net)
  1908. {
  1909. struct datapath *dp, *dp_next;
  1910. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1911. ovs_lock();
  1912. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1913. __dp_destroy(dp);
  1914. ovs_unlock();
  1915. cancel_work_sync(&ovs_net->dp_notify_work);
  1916. }
  1917. static struct pernet_operations ovs_net_ops = {
  1918. .init = ovs_init_net,
  1919. .exit = ovs_exit_net,
  1920. .id = &ovs_net_id,
  1921. .size = sizeof(struct ovs_net),
  1922. };
  1923. static int __init dp_init(void)
  1924. {
  1925. int err;
  1926. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1927. pr_info("Open vSwitch switching datapath\n");
  1928. err = ovs_flow_init();
  1929. if (err)
  1930. goto error;
  1931. err = ovs_vport_init();
  1932. if (err)
  1933. goto error_flow_exit;
  1934. err = register_pernet_device(&ovs_net_ops);
  1935. if (err)
  1936. goto error_vport_exit;
  1937. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1938. if (err)
  1939. goto error_netns_exit;
  1940. err = dp_register_genl();
  1941. if (err < 0)
  1942. goto error_unreg_notifier;
  1943. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1944. return 0;
  1945. error_unreg_notifier:
  1946. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1947. error_netns_exit:
  1948. unregister_pernet_device(&ovs_net_ops);
  1949. error_vport_exit:
  1950. ovs_vport_exit();
  1951. error_flow_exit:
  1952. ovs_flow_exit();
  1953. error:
  1954. return err;
  1955. }
  1956. static void dp_cleanup(void)
  1957. {
  1958. cancel_delayed_work_sync(&rehash_flow_wq);
  1959. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1960. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1961. unregister_pernet_device(&ovs_net_ops);
  1962. rcu_barrier();
  1963. ovs_vport_exit();
  1964. ovs_flow_exit();
  1965. }
  1966. module_init(dp_init);
  1967. module_exit(dp_cleanup);
  1968. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1969. MODULE_LICENSE("GPL");