datapath.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151
  1. /*
  2. * Copyright (c) 2007-2014 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/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <net/genetlink.h>
  49. #include <net/net_namespace.h>
  50. #include <net/netns/generic.h>
  51. #include "datapath.h"
  52. #include "flow.h"
  53. #include "flow_table.h"
  54. #include "flow_netlink.h"
  55. #include "vport-internal_dev.h"
  56. #include "vport-netdev.h"
  57. int ovs_net_id __read_mostly;
  58. static struct genl_family dp_packet_genl_family;
  59. static struct genl_family dp_flow_genl_family;
  60. static struct genl_family dp_datapath_genl_family;
  61. static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
  62. .name = OVS_FLOW_MCGROUP,
  63. };
  64. static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  65. .name = OVS_DATAPATH_MCGROUP,
  66. };
  67. static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
  68. .name = OVS_VPORT_MCGROUP,
  69. };
  70. /* Check if need to build a reply message.
  71. * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
  72. static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
  73. unsigned int group)
  74. {
  75. return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
  76. genl_has_listeners(family, genl_info_net(info)->genl_sock,
  77. group);
  78. }
  79. static void ovs_notify(struct genl_family *family,
  80. struct sk_buff *skb, struct genl_info *info)
  81. {
  82. genl_notify(family, skb, genl_info_net(info), info->snd_portid,
  83. 0, info->nlhdr, GFP_KERNEL);
  84. }
  85. /**
  86. * DOC: Locking:
  87. *
  88. * All writes e.g. Writes to device state (add/remove datapath, port, set
  89. * operations on vports, etc.), Writes to other state (flow table
  90. * modifications, set miscellaneous datapath parameters, etc.) are protected
  91. * by ovs_lock.
  92. *
  93. * Reads are protected by RCU.
  94. *
  95. * There are a few special cases (mostly stats) that have their own
  96. * synchronization but they nest under all of above and don't interact with
  97. * each other.
  98. *
  99. * The RTNL lock nests inside ovs_mutex.
  100. */
  101. static DEFINE_MUTEX(ovs_mutex);
  102. void ovs_lock(void)
  103. {
  104. mutex_lock(&ovs_mutex);
  105. }
  106. void ovs_unlock(void)
  107. {
  108. mutex_unlock(&ovs_mutex);
  109. }
  110. #ifdef CONFIG_LOCKDEP
  111. int lockdep_ovsl_is_held(void)
  112. {
  113. if (debug_locks)
  114. return lockdep_is_held(&ovs_mutex);
  115. else
  116. return 1;
  117. }
  118. #endif
  119. static struct vport *new_vport(const struct vport_parms *);
  120. static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
  121. const struct dp_upcall_info *);
  122. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  123. const struct dp_upcall_info *);
  124. /* Must be called with rcu_read_lock or ovs_mutex. */
  125. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  126. {
  127. struct datapath *dp = NULL;
  128. struct net_device *dev;
  129. rcu_read_lock();
  130. dev = dev_get_by_index_rcu(net, dp_ifindex);
  131. if (dev) {
  132. struct vport *vport = ovs_internal_dev_get_vport(dev);
  133. if (vport)
  134. dp = vport->dp;
  135. }
  136. rcu_read_unlock();
  137. return dp;
  138. }
  139. /* Must be called with rcu_read_lock or ovs_mutex. */
  140. const char *ovs_dp_name(const struct datapath *dp)
  141. {
  142. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  143. return vport->ops->get_name(vport);
  144. }
  145. static int get_dpifindex(struct datapath *dp)
  146. {
  147. struct vport *local;
  148. int ifindex;
  149. rcu_read_lock();
  150. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  151. if (local)
  152. ifindex = netdev_vport_priv(local)->dev->ifindex;
  153. else
  154. ifindex = 0;
  155. rcu_read_unlock();
  156. return ifindex;
  157. }
  158. static void destroy_dp_rcu(struct rcu_head *rcu)
  159. {
  160. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  161. free_percpu(dp->stats_percpu);
  162. release_net(ovs_dp_get_net(dp));
  163. kfree(dp->ports);
  164. kfree(dp);
  165. }
  166. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  167. u16 port_no)
  168. {
  169. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  170. }
  171. /* Called with ovs_mutex or RCU read lock. */
  172. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  173. {
  174. struct vport *vport;
  175. struct hlist_head *head;
  176. head = vport_hash_bucket(dp, port_no);
  177. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  178. if (vport->port_no == port_no)
  179. return vport;
  180. }
  181. return NULL;
  182. }
  183. /* Called with ovs_mutex. */
  184. static struct vport *new_vport(const struct vport_parms *parms)
  185. {
  186. struct vport *vport;
  187. vport = ovs_vport_add(parms);
  188. if (!IS_ERR(vport)) {
  189. struct datapath *dp = parms->dp;
  190. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  191. hlist_add_head_rcu(&vport->dp_hash_node, head);
  192. }
  193. return vport;
  194. }
  195. void ovs_dp_detach_port(struct vport *p)
  196. {
  197. ASSERT_OVSL();
  198. /* First drop references to device. */
  199. hlist_del_rcu(&p->dp_hash_node);
  200. /* Then destroy it. */
  201. ovs_vport_del(p);
  202. }
  203. /* Must be called with rcu_read_lock. */
  204. void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
  205. {
  206. const struct vport *p = OVS_CB(skb)->input_vport;
  207. struct datapath *dp = p->dp;
  208. struct sw_flow *flow;
  209. struct dp_stats_percpu *stats;
  210. u64 *stats_counter;
  211. u32 n_mask_hit;
  212. stats = this_cpu_ptr(dp->stats_percpu);
  213. /* Look up flow. */
  214. flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
  215. if (unlikely(!flow)) {
  216. struct dp_upcall_info upcall;
  217. int error;
  218. upcall.cmd = OVS_PACKET_CMD_MISS;
  219. upcall.key = key;
  220. upcall.userdata = NULL;
  221. upcall.portid = ovs_vport_find_upcall_portid(p, skb);
  222. error = ovs_dp_upcall(dp, skb, &upcall);
  223. if (unlikely(error))
  224. kfree_skb(skb);
  225. else
  226. consume_skb(skb);
  227. stats_counter = &stats->n_missed;
  228. goto out;
  229. }
  230. OVS_CB(skb)->flow = flow;
  231. ovs_flow_stats_update(OVS_CB(skb)->flow, key->tp.flags, skb);
  232. ovs_execute_actions(dp, skb, key);
  233. stats_counter = &stats->n_hit;
  234. out:
  235. /* Update datapath statistics. */
  236. u64_stats_update_begin(&stats->syncp);
  237. (*stats_counter)++;
  238. stats->n_mask_hit += n_mask_hit;
  239. u64_stats_update_end(&stats->syncp);
  240. }
  241. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  242. const struct dp_upcall_info *upcall_info)
  243. {
  244. struct dp_stats_percpu *stats;
  245. int err;
  246. if (upcall_info->portid == 0) {
  247. err = -ENOTCONN;
  248. goto err;
  249. }
  250. if (!skb_is_gso(skb))
  251. err = queue_userspace_packet(dp, skb, upcall_info);
  252. else
  253. err = queue_gso_packets(dp, 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->syncp);
  260. stats->n_lost++;
  261. u64_stats_update_end(&stats->syncp);
  262. return err;
  263. }
  264. static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
  265. const struct dp_upcall_info *upcall_info)
  266. {
  267. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  268. struct dp_upcall_info later_info;
  269. struct sw_flow_key later_key;
  270. struct sk_buff *segs, *nskb;
  271. int err;
  272. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  273. if (IS_ERR(segs))
  274. return PTR_ERR(segs);
  275. /* Queue all of the segments. */
  276. skb = segs;
  277. do {
  278. err = queue_userspace_packet(dp, skb, upcall_info);
  279. if (err)
  280. break;
  281. if (skb == segs && gso_type & SKB_GSO_UDP) {
  282. /* The initial flow key extracted by ovs_flow_extract()
  283. * in this case is for a first fragment, so we need to
  284. * properly mark later fragments.
  285. */
  286. later_key = *upcall_info->key;
  287. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  288. later_info = *upcall_info;
  289. later_info.key = &later_key;
  290. upcall_info = &later_info;
  291. }
  292. } while ((skb = skb->next));
  293. /* Free all of the segments. */
  294. skb = segs;
  295. do {
  296. nskb = skb->next;
  297. if (err)
  298. kfree_skb(skb);
  299. else
  300. consume_skb(skb);
  301. } while ((skb = nskb));
  302. return err;
  303. }
  304. static size_t key_attr_size(void)
  305. {
  306. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  307. + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
  308. + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
  309. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
  310. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
  311. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
  312. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
  313. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
  314. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
  315. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
  316. + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
  317. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  318. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  319. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  320. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  321. + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
  322. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  323. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  324. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  325. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  326. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  327. }
  328. static size_t upcall_msg_size(const struct nlattr *userdata,
  329. unsigned int hdrlen)
  330. {
  331. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  332. + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
  333. + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  334. /* OVS_PACKET_ATTR_USERDATA */
  335. if (userdata)
  336. size += NLA_ALIGN(userdata->nla_len);
  337. return size;
  338. }
  339. static int queue_userspace_packet(struct datapath *dp, 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 = NULL; /* to be queued to userspace */
  345. struct nlattr *nla;
  346. struct genl_info info = {
  347. .dst_sk = ovs_dp_get_net(dp)->genl_sock,
  348. .snd_portid = upcall_info->portid,
  349. };
  350. size_t len;
  351. unsigned int hlen;
  352. int err, dp_ifindex;
  353. dp_ifindex = get_dpifindex(dp);
  354. if (!dp_ifindex)
  355. return -ENODEV;
  356. if (vlan_tx_tag_present(skb)) {
  357. nskb = skb_clone(skb, GFP_ATOMIC);
  358. if (!nskb)
  359. return -ENOMEM;
  360. nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
  361. if (!nskb)
  362. return -ENOMEM;
  363. nskb->vlan_tci = 0;
  364. skb = nskb;
  365. }
  366. if (nla_attr_size(skb->len) > USHRT_MAX) {
  367. err = -EFBIG;
  368. goto out;
  369. }
  370. /* Complete checksum if needed */
  371. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  372. (err = skb_checksum_help(skb)))
  373. goto out;
  374. /* Older versions of OVS user space enforce alignment of the last
  375. * Netlink attribute to NLA_ALIGNTO which would require extensive
  376. * padding logic. Only perform zerocopy if padding is not required.
  377. */
  378. if (dp->user_features & OVS_DP_F_UNALIGNED)
  379. hlen = skb_zerocopy_headlen(skb);
  380. else
  381. hlen = skb->len;
  382. len = upcall_msg_size(upcall_info->userdata, hlen);
  383. user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
  384. if (!user_skb) {
  385. err = -ENOMEM;
  386. goto out;
  387. }
  388. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  389. 0, upcall_info->cmd);
  390. upcall->dp_ifindex = dp_ifindex;
  391. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  392. err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
  393. BUG_ON(err);
  394. nla_nest_end(user_skb, nla);
  395. if (upcall_info->userdata)
  396. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  397. nla_len(upcall_info->userdata),
  398. nla_data(upcall_info->userdata));
  399. /* Only reserve room for attribute header, packet data is added
  400. * in skb_zerocopy() */
  401. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  402. err = -ENOBUFS;
  403. goto out;
  404. }
  405. nla->nla_len = nla_attr_size(skb->len);
  406. err = skb_zerocopy(user_skb, skb, skb->len, hlen);
  407. if (err)
  408. goto out;
  409. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  410. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  411. size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
  412. if (plen > 0)
  413. memset(skb_put(user_skb, plen), 0, plen);
  414. }
  415. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  416. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  417. user_skb = NULL;
  418. out:
  419. if (err)
  420. skb_tx_error(skb);
  421. kfree_skb(user_skb);
  422. kfree_skb(nskb);
  423. return err;
  424. }
  425. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  426. {
  427. struct ovs_header *ovs_header = info->userhdr;
  428. struct nlattr **a = info->attrs;
  429. struct sw_flow_actions *acts;
  430. struct sk_buff *packet;
  431. struct sw_flow *flow;
  432. struct datapath *dp;
  433. struct ethhdr *eth;
  434. struct vport *input_vport;
  435. int len;
  436. int err;
  437. err = -EINVAL;
  438. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  439. !a[OVS_PACKET_ATTR_ACTIONS])
  440. goto err;
  441. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  442. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  443. err = -ENOMEM;
  444. if (!packet)
  445. goto err;
  446. skb_reserve(packet, NET_IP_ALIGN);
  447. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  448. skb_reset_mac_header(packet);
  449. eth = eth_hdr(packet);
  450. /* Normally, setting the skb 'protocol' field would be handled by a
  451. * call to eth_type_trans(), but it assumes there's a sending
  452. * device, which we may not have. */
  453. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  454. packet->protocol = eth->h_proto;
  455. else
  456. packet->protocol = htons(ETH_P_802_2);
  457. /* Build an sw_flow for sending this packet. */
  458. flow = ovs_flow_alloc();
  459. err = PTR_ERR(flow);
  460. if (IS_ERR(flow))
  461. goto err_kfree_skb;
  462. err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
  463. &flow->key);
  464. if (err)
  465. goto err_flow_free;
  466. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
  467. err = PTR_ERR(acts);
  468. if (IS_ERR(acts))
  469. goto err_flow_free;
  470. err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
  471. &flow->key, 0, &acts);
  472. if (err)
  473. goto err_flow_free;
  474. rcu_assign_pointer(flow->sf_acts, acts);
  475. OVS_CB(packet)->egress_tun_info = NULL;
  476. OVS_CB(packet)->flow = flow;
  477. packet->priority = flow->key.phy.priority;
  478. packet->mark = flow->key.phy.skb_mark;
  479. rcu_read_lock();
  480. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  481. err = -ENODEV;
  482. if (!dp)
  483. goto err_unlock;
  484. input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
  485. if (!input_vport)
  486. input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
  487. if (!input_vport)
  488. goto err_unlock;
  489. OVS_CB(packet)->input_vport = input_vport;
  490. local_bh_disable();
  491. err = ovs_execute_actions(dp, packet, &flow->key);
  492. local_bh_enable();
  493. rcu_read_unlock();
  494. ovs_flow_free(flow, false);
  495. return err;
  496. err_unlock:
  497. rcu_read_unlock();
  498. err_flow_free:
  499. ovs_flow_free(flow, false);
  500. err_kfree_skb:
  501. kfree_skb(packet);
  502. err:
  503. return err;
  504. }
  505. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  506. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  507. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  508. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  509. };
  510. static const struct genl_ops dp_packet_genl_ops[] = {
  511. { .cmd = OVS_PACKET_CMD_EXECUTE,
  512. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  513. .policy = packet_policy,
  514. .doit = ovs_packet_cmd_execute
  515. }
  516. };
  517. static struct genl_family dp_packet_genl_family = {
  518. .id = GENL_ID_GENERATE,
  519. .hdrsize = sizeof(struct ovs_header),
  520. .name = OVS_PACKET_FAMILY,
  521. .version = OVS_PACKET_VERSION,
  522. .maxattr = OVS_PACKET_ATTR_MAX,
  523. .netnsok = true,
  524. .parallel_ops = true,
  525. .ops = dp_packet_genl_ops,
  526. .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
  527. };
  528. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
  529. struct ovs_dp_megaflow_stats *mega_stats)
  530. {
  531. int i;
  532. memset(mega_stats, 0, sizeof(*mega_stats));
  533. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  534. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  535. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  536. for_each_possible_cpu(i) {
  537. const struct dp_stats_percpu *percpu_stats;
  538. struct dp_stats_percpu local_stats;
  539. unsigned int start;
  540. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  541. do {
  542. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  543. local_stats = *percpu_stats;
  544. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  545. stats->n_hit += local_stats.n_hit;
  546. stats->n_missed += local_stats.n_missed;
  547. stats->n_lost += local_stats.n_lost;
  548. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  549. }
  550. }
  551. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  552. {
  553. return NLMSG_ALIGN(sizeof(struct ovs_header))
  554. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  555. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
  556. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  557. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  558. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  559. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  560. }
  561. /* Called with ovs_mutex or RCU read lock. */
  562. static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
  563. struct sk_buff *skb, u32 portid,
  564. u32 seq, u32 flags, u8 cmd)
  565. {
  566. const int skb_orig_len = skb->len;
  567. struct nlattr *start;
  568. struct ovs_flow_stats stats;
  569. __be16 tcp_flags;
  570. unsigned long used;
  571. struct ovs_header *ovs_header;
  572. struct nlattr *nla;
  573. int err;
  574. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  575. if (!ovs_header)
  576. return -EMSGSIZE;
  577. ovs_header->dp_ifindex = dp_ifindex;
  578. /* Fill flow key. */
  579. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  580. if (!nla)
  581. goto nla_put_failure;
  582. err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
  583. if (err)
  584. goto error;
  585. nla_nest_end(skb, nla);
  586. nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
  587. if (!nla)
  588. goto nla_put_failure;
  589. err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
  590. if (err)
  591. goto error;
  592. nla_nest_end(skb, nla);
  593. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  594. if (used &&
  595. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  596. goto nla_put_failure;
  597. if (stats.n_packets &&
  598. nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
  599. goto nla_put_failure;
  600. if ((u8)ntohs(tcp_flags) &&
  601. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  602. goto nla_put_failure;
  603. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  604. * this is the first flow to be dumped into 'skb'. This is unusual for
  605. * Netlink but individual action lists can be longer than
  606. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  607. * The userspace caller can always fetch the actions separately if it
  608. * really wants them. (Most userspace callers in fact don't care.)
  609. *
  610. * This can only fail for dump operations because the skb is always
  611. * properly sized for single flows.
  612. */
  613. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  614. if (start) {
  615. const struct sw_flow_actions *sf_acts;
  616. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  617. err = ovs_nla_put_actions(sf_acts->actions,
  618. sf_acts->actions_len, skb);
  619. if (!err)
  620. nla_nest_end(skb, start);
  621. else {
  622. if (skb_orig_len)
  623. goto error;
  624. nla_nest_cancel(skb, start);
  625. }
  626. } else if (skb_orig_len)
  627. goto nla_put_failure;
  628. return genlmsg_end(skb, ovs_header);
  629. nla_put_failure:
  630. err = -EMSGSIZE;
  631. error:
  632. genlmsg_cancel(skb, ovs_header);
  633. return err;
  634. }
  635. /* May not be called with RCU read lock. */
  636. static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
  637. struct genl_info *info,
  638. bool always)
  639. {
  640. struct sk_buff *skb;
  641. if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
  642. return NULL;
  643. skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
  644. if (!skb)
  645. return ERR_PTR(-ENOMEM);
  646. return skb;
  647. }
  648. /* Called with ovs_mutex. */
  649. static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
  650. int dp_ifindex,
  651. struct genl_info *info, u8 cmd,
  652. bool always)
  653. {
  654. struct sk_buff *skb;
  655. int retval;
  656. skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
  657. always);
  658. if (IS_ERR_OR_NULL(skb))
  659. return skb;
  660. retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
  661. info->snd_portid, info->snd_seq, 0,
  662. cmd);
  663. BUG_ON(retval < 0);
  664. return skb;
  665. }
  666. static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
  667. {
  668. struct nlattr **a = info->attrs;
  669. struct ovs_header *ovs_header = info->userhdr;
  670. struct sw_flow *flow, *new_flow;
  671. struct sw_flow_mask mask;
  672. struct sk_buff *reply;
  673. struct datapath *dp;
  674. struct sw_flow_actions *acts;
  675. struct sw_flow_match match;
  676. int error;
  677. /* Must have key and actions. */
  678. error = -EINVAL;
  679. if (!a[OVS_FLOW_ATTR_KEY])
  680. goto error;
  681. if (!a[OVS_FLOW_ATTR_ACTIONS])
  682. goto error;
  683. /* Most of the time we need to allocate a new flow, do it before
  684. * locking.
  685. */
  686. new_flow = ovs_flow_alloc();
  687. if (IS_ERR(new_flow)) {
  688. error = PTR_ERR(new_flow);
  689. goto error;
  690. }
  691. /* Extract key. */
  692. ovs_match_init(&match, &new_flow->unmasked_key, &mask);
  693. error = ovs_nla_get_match(&match,
  694. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  695. if (error)
  696. goto err_kfree_flow;
  697. ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
  698. /* Validate actions. */
  699. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
  700. error = PTR_ERR(acts);
  701. if (IS_ERR(acts))
  702. goto err_kfree_flow;
  703. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
  704. 0, &acts);
  705. if (error) {
  706. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  707. goto err_kfree_acts;
  708. }
  709. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  710. if (IS_ERR(reply)) {
  711. error = PTR_ERR(reply);
  712. goto err_kfree_acts;
  713. }
  714. ovs_lock();
  715. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  716. if (unlikely(!dp)) {
  717. error = -ENODEV;
  718. goto err_unlock_ovs;
  719. }
  720. /* Check if this is a duplicate flow */
  721. flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
  722. if (likely(!flow)) {
  723. rcu_assign_pointer(new_flow->sf_acts, acts);
  724. /* Put flow in bucket. */
  725. error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
  726. if (unlikely(error)) {
  727. acts = NULL;
  728. goto err_unlock_ovs;
  729. }
  730. if (unlikely(reply)) {
  731. error = ovs_flow_cmd_fill_info(new_flow,
  732. ovs_header->dp_ifindex,
  733. reply, info->snd_portid,
  734. info->snd_seq, 0,
  735. OVS_FLOW_CMD_NEW);
  736. BUG_ON(error < 0);
  737. }
  738. ovs_unlock();
  739. } else {
  740. struct sw_flow_actions *old_acts;
  741. /* Bail out if we're not allowed to modify an existing flow.
  742. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  743. * because Generic Netlink treats the latter as a dump
  744. * request. We also accept NLM_F_EXCL in case that bug ever
  745. * gets fixed.
  746. */
  747. if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
  748. | NLM_F_EXCL))) {
  749. error = -EEXIST;
  750. goto err_unlock_ovs;
  751. }
  752. /* The unmasked key has to be the same for flow updates. */
  753. if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
  754. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  755. if (!flow) {
  756. error = -ENOENT;
  757. goto err_unlock_ovs;
  758. }
  759. }
  760. /* Update actions. */
  761. old_acts = ovsl_dereference(flow->sf_acts);
  762. rcu_assign_pointer(flow->sf_acts, acts);
  763. if (unlikely(reply)) {
  764. error = ovs_flow_cmd_fill_info(flow,
  765. ovs_header->dp_ifindex,
  766. reply, info->snd_portid,
  767. info->snd_seq, 0,
  768. OVS_FLOW_CMD_NEW);
  769. BUG_ON(error < 0);
  770. }
  771. ovs_unlock();
  772. ovs_nla_free_flow_actions(old_acts);
  773. ovs_flow_free(new_flow, false);
  774. }
  775. if (reply)
  776. ovs_notify(&dp_flow_genl_family, reply, info);
  777. return 0;
  778. err_unlock_ovs:
  779. ovs_unlock();
  780. kfree_skb(reply);
  781. err_kfree_acts:
  782. kfree(acts);
  783. err_kfree_flow:
  784. ovs_flow_free(new_flow, false);
  785. error:
  786. return error;
  787. }
  788. static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
  789. const struct sw_flow_key *key,
  790. const struct sw_flow_mask *mask)
  791. {
  792. struct sw_flow_actions *acts;
  793. struct sw_flow_key masked_key;
  794. int error;
  795. acts = ovs_nla_alloc_flow_actions(nla_len(a));
  796. if (IS_ERR(acts))
  797. return acts;
  798. ovs_flow_mask_key(&masked_key, key, mask);
  799. error = ovs_nla_copy_actions(a, &masked_key, 0, &acts);
  800. if (error) {
  801. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  802. kfree(acts);
  803. return ERR_PTR(error);
  804. }
  805. return acts;
  806. }
  807. static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
  808. {
  809. struct nlattr **a = info->attrs;
  810. struct ovs_header *ovs_header = info->userhdr;
  811. struct sw_flow_key key;
  812. struct sw_flow *flow;
  813. struct sw_flow_mask mask;
  814. struct sk_buff *reply = NULL;
  815. struct datapath *dp;
  816. struct sw_flow_actions *old_acts = NULL, *acts = NULL;
  817. struct sw_flow_match match;
  818. int error;
  819. /* Extract key. */
  820. error = -EINVAL;
  821. if (!a[OVS_FLOW_ATTR_KEY])
  822. goto error;
  823. ovs_match_init(&match, &key, &mask);
  824. error = ovs_nla_get_match(&match,
  825. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  826. if (error)
  827. goto error;
  828. /* Validate actions. */
  829. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  830. acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
  831. if (IS_ERR(acts)) {
  832. error = PTR_ERR(acts);
  833. goto error;
  834. }
  835. }
  836. /* Can allocate before locking if have acts. */
  837. if (acts) {
  838. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  839. if (IS_ERR(reply)) {
  840. error = PTR_ERR(reply);
  841. goto err_kfree_acts;
  842. }
  843. }
  844. ovs_lock();
  845. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  846. if (unlikely(!dp)) {
  847. error = -ENODEV;
  848. goto err_unlock_ovs;
  849. }
  850. /* Check that the flow exists. */
  851. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  852. if (unlikely(!flow)) {
  853. error = -ENOENT;
  854. goto err_unlock_ovs;
  855. }
  856. /* Update actions, if present. */
  857. if (likely(acts)) {
  858. old_acts = ovsl_dereference(flow->sf_acts);
  859. rcu_assign_pointer(flow->sf_acts, acts);
  860. if (unlikely(reply)) {
  861. error = ovs_flow_cmd_fill_info(flow,
  862. ovs_header->dp_ifindex,
  863. reply, info->snd_portid,
  864. info->snd_seq, 0,
  865. OVS_FLOW_CMD_NEW);
  866. BUG_ON(error < 0);
  867. }
  868. } else {
  869. /* Could not alloc without acts before locking. */
  870. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
  871. info, OVS_FLOW_CMD_NEW, false);
  872. if (unlikely(IS_ERR(reply))) {
  873. error = PTR_ERR(reply);
  874. goto err_unlock_ovs;
  875. }
  876. }
  877. /* Clear stats. */
  878. if (a[OVS_FLOW_ATTR_CLEAR])
  879. ovs_flow_stats_clear(flow);
  880. ovs_unlock();
  881. if (reply)
  882. ovs_notify(&dp_flow_genl_family, reply, info);
  883. if (old_acts)
  884. ovs_nla_free_flow_actions(old_acts);
  885. return 0;
  886. err_unlock_ovs:
  887. ovs_unlock();
  888. kfree_skb(reply);
  889. err_kfree_acts:
  890. kfree(acts);
  891. error:
  892. return error;
  893. }
  894. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  895. {
  896. struct nlattr **a = info->attrs;
  897. struct ovs_header *ovs_header = info->userhdr;
  898. struct sw_flow_key key;
  899. struct sk_buff *reply;
  900. struct sw_flow *flow;
  901. struct datapath *dp;
  902. struct sw_flow_match match;
  903. int err;
  904. if (!a[OVS_FLOW_ATTR_KEY]) {
  905. OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
  906. return -EINVAL;
  907. }
  908. ovs_match_init(&match, &key, NULL);
  909. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  910. if (err)
  911. return err;
  912. ovs_lock();
  913. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  914. if (!dp) {
  915. err = -ENODEV;
  916. goto unlock;
  917. }
  918. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  919. if (!flow) {
  920. err = -ENOENT;
  921. goto unlock;
  922. }
  923. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
  924. OVS_FLOW_CMD_NEW, true);
  925. if (IS_ERR(reply)) {
  926. err = PTR_ERR(reply);
  927. goto unlock;
  928. }
  929. ovs_unlock();
  930. return genlmsg_reply(reply, info);
  931. unlock:
  932. ovs_unlock();
  933. return err;
  934. }
  935. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  936. {
  937. struct nlattr **a = info->attrs;
  938. struct ovs_header *ovs_header = info->userhdr;
  939. struct sw_flow_key key;
  940. struct sk_buff *reply;
  941. struct sw_flow *flow;
  942. struct datapath *dp;
  943. struct sw_flow_match match;
  944. int err;
  945. if (likely(a[OVS_FLOW_ATTR_KEY])) {
  946. ovs_match_init(&match, &key, NULL);
  947. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  948. if (unlikely(err))
  949. return err;
  950. }
  951. ovs_lock();
  952. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  953. if (unlikely(!dp)) {
  954. err = -ENODEV;
  955. goto unlock;
  956. }
  957. if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
  958. err = ovs_flow_tbl_flush(&dp->table);
  959. goto unlock;
  960. }
  961. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  962. if (unlikely(!flow)) {
  963. err = -ENOENT;
  964. goto unlock;
  965. }
  966. ovs_flow_tbl_remove(&dp->table, flow);
  967. ovs_unlock();
  968. reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
  969. info, false);
  970. if (likely(reply)) {
  971. if (likely(!IS_ERR(reply))) {
  972. rcu_read_lock(); /*To keep RCU checker happy. */
  973. err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
  974. reply, info->snd_portid,
  975. info->snd_seq, 0,
  976. OVS_FLOW_CMD_DEL);
  977. rcu_read_unlock();
  978. BUG_ON(err < 0);
  979. ovs_notify(&dp_flow_genl_family, reply, info);
  980. } else {
  981. netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
  982. }
  983. }
  984. ovs_flow_free(flow, true);
  985. return 0;
  986. unlock:
  987. ovs_unlock();
  988. return err;
  989. }
  990. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  991. {
  992. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  993. struct table_instance *ti;
  994. struct datapath *dp;
  995. rcu_read_lock();
  996. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  997. if (!dp) {
  998. rcu_read_unlock();
  999. return -ENODEV;
  1000. }
  1001. ti = rcu_dereference(dp->table.ti);
  1002. for (;;) {
  1003. struct sw_flow *flow;
  1004. u32 bucket, obj;
  1005. bucket = cb->args[0];
  1006. obj = cb->args[1];
  1007. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  1008. if (!flow)
  1009. break;
  1010. if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
  1011. NETLINK_CB(cb->skb).portid,
  1012. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1013. OVS_FLOW_CMD_NEW) < 0)
  1014. break;
  1015. cb->args[0] = bucket;
  1016. cb->args[1] = obj;
  1017. }
  1018. rcu_read_unlock();
  1019. return skb->len;
  1020. }
  1021. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  1022. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  1023. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  1024. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  1025. };
  1026. static const struct genl_ops dp_flow_genl_ops[] = {
  1027. { .cmd = OVS_FLOW_CMD_NEW,
  1028. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1029. .policy = flow_policy,
  1030. .doit = ovs_flow_cmd_new
  1031. },
  1032. { .cmd = OVS_FLOW_CMD_DEL,
  1033. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1034. .policy = flow_policy,
  1035. .doit = ovs_flow_cmd_del
  1036. },
  1037. { .cmd = OVS_FLOW_CMD_GET,
  1038. .flags = 0, /* OK for unprivileged users. */
  1039. .policy = flow_policy,
  1040. .doit = ovs_flow_cmd_get,
  1041. .dumpit = ovs_flow_cmd_dump
  1042. },
  1043. { .cmd = OVS_FLOW_CMD_SET,
  1044. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1045. .policy = flow_policy,
  1046. .doit = ovs_flow_cmd_set,
  1047. },
  1048. };
  1049. static struct genl_family dp_flow_genl_family = {
  1050. .id = GENL_ID_GENERATE,
  1051. .hdrsize = sizeof(struct ovs_header),
  1052. .name = OVS_FLOW_FAMILY,
  1053. .version = OVS_FLOW_VERSION,
  1054. .maxattr = OVS_FLOW_ATTR_MAX,
  1055. .netnsok = true,
  1056. .parallel_ops = true,
  1057. .ops = dp_flow_genl_ops,
  1058. .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
  1059. .mcgrps = &ovs_dp_flow_multicast_group,
  1060. .n_mcgrps = 1,
  1061. };
  1062. static size_t ovs_dp_cmd_msg_size(void)
  1063. {
  1064. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1065. msgsize += nla_total_size(IFNAMSIZ);
  1066. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  1067. msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
  1068. msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
  1069. return msgsize;
  1070. }
  1071. /* Called with ovs_mutex or RCU read lock. */
  1072. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1073. u32 portid, u32 seq, u32 flags, u8 cmd)
  1074. {
  1075. struct ovs_header *ovs_header;
  1076. struct ovs_dp_stats dp_stats;
  1077. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  1078. int err;
  1079. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1080. flags, cmd);
  1081. if (!ovs_header)
  1082. goto error;
  1083. ovs_header->dp_ifindex = get_dpifindex(dp);
  1084. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1085. if (err)
  1086. goto nla_put_failure;
  1087. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  1088. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  1089. &dp_stats))
  1090. goto nla_put_failure;
  1091. if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  1092. sizeof(struct ovs_dp_megaflow_stats),
  1093. &dp_megaflow_stats))
  1094. goto nla_put_failure;
  1095. if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
  1096. goto nla_put_failure;
  1097. return genlmsg_end(skb, ovs_header);
  1098. nla_put_failure:
  1099. genlmsg_cancel(skb, ovs_header);
  1100. error:
  1101. return -EMSGSIZE;
  1102. }
  1103. static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
  1104. {
  1105. return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
  1106. }
  1107. /* Called with rcu_read_lock or ovs_mutex. */
  1108. static struct datapath *lookup_datapath(struct net *net,
  1109. struct ovs_header *ovs_header,
  1110. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1111. {
  1112. struct datapath *dp;
  1113. if (!a[OVS_DP_ATTR_NAME])
  1114. dp = get_dp(net, ovs_header->dp_ifindex);
  1115. else {
  1116. struct vport *vport;
  1117. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1118. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1119. }
  1120. return dp ? dp : ERR_PTR(-ENODEV);
  1121. }
  1122. static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
  1123. {
  1124. struct datapath *dp;
  1125. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1126. if (IS_ERR(dp))
  1127. return;
  1128. WARN(dp->user_features, "Dropping previously announced user features\n");
  1129. dp->user_features = 0;
  1130. }
  1131. static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
  1132. {
  1133. if (a[OVS_DP_ATTR_USER_FEATURES])
  1134. dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
  1135. }
  1136. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1137. {
  1138. struct nlattr **a = info->attrs;
  1139. struct vport_parms parms;
  1140. struct sk_buff *reply;
  1141. struct datapath *dp;
  1142. struct vport *vport;
  1143. struct ovs_net *ovs_net;
  1144. int err, i;
  1145. err = -EINVAL;
  1146. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1147. goto err;
  1148. reply = ovs_dp_cmd_alloc_info(info);
  1149. if (!reply)
  1150. return -ENOMEM;
  1151. err = -ENOMEM;
  1152. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1153. if (dp == NULL)
  1154. goto err_free_reply;
  1155. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1156. /* Allocate table. */
  1157. err = ovs_flow_tbl_init(&dp->table);
  1158. if (err)
  1159. goto err_free_dp;
  1160. dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
  1161. if (!dp->stats_percpu) {
  1162. err = -ENOMEM;
  1163. goto err_destroy_table;
  1164. }
  1165. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1166. GFP_KERNEL);
  1167. if (!dp->ports) {
  1168. err = -ENOMEM;
  1169. goto err_destroy_percpu;
  1170. }
  1171. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1172. INIT_HLIST_HEAD(&dp->ports[i]);
  1173. /* Set up our datapath device. */
  1174. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1175. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1176. parms.options = NULL;
  1177. parms.dp = dp;
  1178. parms.port_no = OVSP_LOCAL;
  1179. parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
  1180. ovs_dp_change(dp, a);
  1181. /* So far only local changes have been made, now need the lock. */
  1182. ovs_lock();
  1183. vport = new_vport(&parms);
  1184. if (IS_ERR(vport)) {
  1185. err = PTR_ERR(vport);
  1186. if (err == -EBUSY)
  1187. err = -EEXIST;
  1188. if (err == -EEXIST) {
  1189. /* An outdated user space instance that does not understand
  1190. * the concept of user_features has attempted to create a new
  1191. * datapath and is likely to reuse it. Drop all user features.
  1192. */
  1193. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1194. ovs_dp_reset_user_features(skb, info);
  1195. }
  1196. goto err_destroy_ports_array;
  1197. }
  1198. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1199. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1200. BUG_ON(err < 0);
  1201. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1202. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1203. ovs_unlock();
  1204. ovs_notify(&dp_datapath_genl_family, reply, info);
  1205. return 0;
  1206. err_destroy_ports_array:
  1207. ovs_unlock();
  1208. kfree(dp->ports);
  1209. err_destroy_percpu:
  1210. free_percpu(dp->stats_percpu);
  1211. err_destroy_table:
  1212. ovs_flow_tbl_destroy(&dp->table, false);
  1213. err_free_dp:
  1214. release_net(ovs_dp_get_net(dp));
  1215. kfree(dp);
  1216. err_free_reply:
  1217. kfree_skb(reply);
  1218. err:
  1219. return err;
  1220. }
  1221. /* Called with ovs_mutex. */
  1222. static void __dp_destroy(struct datapath *dp)
  1223. {
  1224. int i;
  1225. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1226. struct vport *vport;
  1227. struct hlist_node *n;
  1228. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1229. if (vport->port_no != OVSP_LOCAL)
  1230. ovs_dp_detach_port(vport);
  1231. }
  1232. list_del_rcu(&dp->list_node);
  1233. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1234. * all ports in datapath are destroyed first before freeing datapath.
  1235. */
  1236. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1237. /* RCU destroy the flow table */
  1238. ovs_flow_tbl_destroy(&dp->table, true);
  1239. call_rcu(&dp->rcu, destroy_dp_rcu);
  1240. }
  1241. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1242. {
  1243. struct sk_buff *reply;
  1244. struct datapath *dp;
  1245. int err;
  1246. reply = ovs_dp_cmd_alloc_info(info);
  1247. if (!reply)
  1248. return -ENOMEM;
  1249. ovs_lock();
  1250. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1251. err = PTR_ERR(dp);
  1252. if (IS_ERR(dp))
  1253. goto err_unlock_free;
  1254. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1255. info->snd_seq, 0, OVS_DP_CMD_DEL);
  1256. BUG_ON(err < 0);
  1257. __dp_destroy(dp);
  1258. ovs_unlock();
  1259. ovs_notify(&dp_datapath_genl_family, reply, info);
  1260. return 0;
  1261. err_unlock_free:
  1262. ovs_unlock();
  1263. kfree_skb(reply);
  1264. return err;
  1265. }
  1266. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1267. {
  1268. struct sk_buff *reply;
  1269. struct datapath *dp;
  1270. int err;
  1271. reply = ovs_dp_cmd_alloc_info(info);
  1272. if (!reply)
  1273. return -ENOMEM;
  1274. ovs_lock();
  1275. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1276. err = PTR_ERR(dp);
  1277. if (IS_ERR(dp))
  1278. goto err_unlock_free;
  1279. ovs_dp_change(dp, info->attrs);
  1280. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1281. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1282. BUG_ON(err < 0);
  1283. ovs_unlock();
  1284. ovs_notify(&dp_datapath_genl_family, reply, info);
  1285. return 0;
  1286. err_unlock_free:
  1287. ovs_unlock();
  1288. kfree_skb(reply);
  1289. return err;
  1290. }
  1291. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1292. {
  1293. struct sk_buff *reply;
  1294. struct datapath *dp;
  1295. int err;
  1296. reply = ovs_dp_cmd_alloc_info(info);
  1297. if (!reply)
  1298. return -ENOMEM;
  1299. rcu_read_lock();
  1300. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1301. if (IS_ERR(dp)) {
  1302. err = PTR_ERR(dp);
  1303. goto err_unlock_free;
  1304. }
  1305. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1306. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1307. BUG_ON(err < 0);
  1308. rcu_read_unlock();
  1309. return genlmsg_reply(reply, info);
  1310. err_unlock_free:
  1311. rcu_read_unlock();
  1312. kfree_skb(reply);
  1313. return err;
  1314. }
  1315. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1316. {
  1317. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1318. struct datapath *dp;
  1319. int skip = cb->args[0];
  1320. int i = 0;
  1321. rcu_read_lock();
  1322. list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
  1323. if (i >= skip &&
  1324. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1325. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1326. OVS_DP_CMD_NEW) < 0)
  1327. break;
  1328. i++;
  1329. }
  1330. rcu_read_unlock();
  1331. cb->args[0] = i;
  1332. return skb->len;
  1333. }
  1334. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1335. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1336. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1337. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  1338. };
  1339. static const struct genl_ops dp_datapath_genl_ops[] = {
  1340. { .cmd = OVS_DP_CMD_NEW,
  1341. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1342. .policy = datapath_policy,
  1343. .doit = ovs_dp_cmd_new
  1344. },
  1345. { .cmd = OVS_DP_CMD_DEL,
  1346. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1347. .policy = datapath_policy,
  1348. .doit = ovs_dp_cmd_del
  1349. },
  1350. { .cmd = OVS_DP_CMD_GET,
  1351. .flags = 0, /* OK for unprivileged users. */
  1352. .policy = datapath_policy,
  1353. .doit = ovs_dp_cmd_get,
  1354. .dumpit = ovs_dp_cmd_dump
  1355. },
  1356. { .cmd = OVS_DP_CMD_SET,
  1357. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1358. .policy = datapath_policy,
  1359. .doit = ovs_dp_cmd_set,
  1360. },
  1361. };
  1362. static struct genl_family dp_datapath_genl_family = {
  1363. .id = GENL_ID_GENERATE,
  1364. .hdrsize = sizeof(struct ovs_header),
  1365. .name = OVS_DATAPATH_FAMILY,
  1366. .version = OVS_DATAPATH_VERSION,
  1367. .maxattr = OVS_DP_ATTR_MAX,
  1368. .netnsok = true,
  1369. .parallel_ops = true,
  1370. .ops = dp_datapath_genl_ops,
  1371. .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
  1372. .mcgrps = &ovs_dp_datapath_multicast_group,
  1373. .n_mcgrps = 1,
  1374. };
  1375. /* Called with ovs_mutex or RCU read lock. */
  1376. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1377. u32 portid, u32 seq, u32 flags, u8 cmd)
  1378. {
  1379. struct ovs_header *ovs_header;
  1380. struct ovs_vport_stats vport_stats;
  1381. int err;
  1382. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1383. flags, cmd);
  1384. if (!ovs_header)
  1385. return -EMSGSIZE;
  1386. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1387. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1388. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1389. nla_put_string(skb, OVS_VPORT_ATTR_NAME,
  1390. vport->ops->get_name(vport)))
  1391. goto nla_put_failure;
  1392. ovs_vport_get_stats(vport, &vport_stats);
  1393. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1394. &vport_stats))
  1395. goto nla_put_failure;
  1396. if (ovs_vport_get_upcall_portids(vport, skb))
  1397. goto nla_put_failure;
  1398. err = ovs_vport_get_options(vport, skb);
  1399. if (err == -EMSGSIZE)
  1400. goto error;
  1401. return genlmsg_end(skb, ovs_header);
  1402. nla_put_failure:
  1403. err = -EMSGSIZE;
  1404. error:
  1405. genlmsg_cancel(skb, ovs_header);
  1406. return err;
  1407. }
  1408. static struct sk_buff *ovs_vport_cmd_alloc_info(void)
  1409. {
  1410. return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1411. }
  1412. /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
  1413. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1414. u32 seq, u8 cmd)
  1415. {
  1416. struct sk_buff *skb;
  1417. int retval;
  1418. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1419. if (!skb)
  1420. return ERR_PTR(-ENOMEM);
  1421. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1422. BUG_ON(retval < 0);
  1423. return skb;
  1424. }
  1425. /* Called with ovs_mutex or RCU read lock. */
  1426. static struct vport *lookup_vport(struct net *net,
  1427. struct ovs_header *ovs_header,
  1428. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1429. {
  1430. struct datapath *dp;
  1431. struct vport *vport;
  1432. if (a[OVS_VPORT_ATTR_NAME]) {
  1433. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1434. if (!vport)
  1435. return ERR_PTR(-ENODEV);
  1436. if (ovs_header->dp_ifindex &&
  1437. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1438. return ERR_PTR(-ENODEV);
  1439. return vport;
  1440. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1441. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1442. if (port_no >= DP_MAX_PORTS)
  1443. return ERR_PTR(-EFBIG);
  1444. dp = get_dp(net, ovs_header->dp_ifindex);
  1445. if (!dp)
  1446. return ERR_PTR(-ENODEV);
  1447. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1448. if (!vport)
  1449. return ERR_PTR(-ENODEV);
  1450. return vport;
  1451. } else
  1452. return ERR_PTR(-EINVAL);
  1453. }
  1454. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1455. {
  1456. struct nlattr **a = info->attrs;
  1457. struct ovs_header *ovs_header = info->userhdr;
  1458. struct vport_parms parms;
  1459. struct sk_buff *reply;
  1460. struct vport *vport;
  1461. struct datapath *dp;
  1462. u32 port_no;
  1463. int err;
  1464. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1465. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1466. return -EINVAL;
  1467. port_no = a[OVS_VPORT_ATTR_PORT_NO]
  1468. ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
  1469. if (port_no >= DP_MAX_PORTS)
  1470. return -EFBIG;
  1471. reply = ovs_vport_cmd_alloc_info();
  1472. if (!reply)
  1473. return -ENOMEM;
  1474. ovs_lock();
  1475. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1476. err = -ENODEV;
  1477. if (!dp)
  1478. goto exit_unlock_free;
  1479. if (port_no) {
  1480. vport = ovs_vport_ovsl(dp, port_no);
  1481. err = -EBUSY;
  1482. if (vport)
  1483. goto exit_unlock_free;
  1484. } else {
  1485. for (port_no = 1; ; port_no++) {
  1486. if (port_no >= DP_MAX_PORTS) {
  1487. err = -EFBIG;
  1488. goto exit_unlock_free;
  1489. }
  1490. vport = ovs_vport_ovsl(dp, port_no);
  1491. if (!vport)
  1492. break;
  1493. }
  1494. }
  1495. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1496. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1497. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1498. parms.dp = dp;
  1499. parms.port_no = port_no;
  1500. parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1501. vport = new_vport(&parms);
  1502. err = PTR_ERR(vport);
  1503. if (IS_ERR(vport))
  1504. goto exit_unlock_free;
  1505. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1506. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1507. BUG_ON(err < 0);
  1508. ovs_unlock();
  1509. ovs_notify(&dp_vport_genl_family, reply, info);
  1510. return 0;
  1511. exit_unlock_free:
  1512. ovs_unlock();
  1513. kfree_skb(reply);
  1514. return err;
  1515. }
  1516. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1517. {
  1518. struct nlattr **a = info->attrs;
  1519. struct sk_buff *reply;
  1520. struct vport *vport;
  1521. int err;
  1522. reply = ovs_vport_cmd_alloc_info();
  1523. if (!reply)
  1524. return -ENOMEM;
  1525. ovs_lock();
  1526. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1527. err = PTR_ERR(vport);
  1528. if (IS_ERR(vport))
  1529. goto exit_unlock_free;
  1530. if (a[OVS_VPORT_ATTR_TYPE] &&
  1531. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1532. err = -EINVAL;
  1533. goto exit_unlock_free;
  1534. }
  1535. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1536. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1537. if (err)
  1538. goto exit_unlock_free;
  1539. }
  1540. if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
  1541. struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1542. err = ovs_vport_set_upcall_portids(vport, ids);
  1543. if (err)
  1544. goto exit_unlock_free;
  1545. }
  1546. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1547. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1548. BUG_ON(err < 0);
  1549. ovs_unlock();
  1550. ovs_notify(&dp_vport_genl_family, reply, info);
  1551. return 0;
  1552. exit_unlock_free:
  1553. ovs_unlock();
  1554. kfree_skb(reply);
  1555. return err;
  1556. }
  1557. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1558. {
  1559. struct nlattr **a = info->attrs;
  1560. struct sk_buff *reply;
  1561. struct vport *vport;
  1562. int err;
  1563. reply = ovs_vport_cmd_alloc_info();
  1564. if (!reply)
  1565. return -ENOMEM;
  1566. ovs_lock();
  1567. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1568. err = PTR_ERR(vport);
  1569. if (IS_ERR(vport))
  1570. goto exit_unlock_free;
  1571. if (vport->port_no == OVSP_LOCAL) {
  1572. err = -EINVAL;
  1573. goto exit_unlock_free;
  1574. }
  1575. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1576. info->snd_seq, 0, OVS_VPORT_CMD_DEL);
  1577. BUG_ON(err < 0);
  1578. ovs_dp_detach_port(vport);
  1579. ovs_unlock();
  1580. ovs_notify(&dp_vport_genl_family, reply, info);
  1581. return 0;
  1582. exit_unlock_free:
  1583. ovs_unlock();
  1584. kfree_skb(reply);
  1585. return err;
  1586. }
  1587. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1588. {
  1589. struct nlattr **a = info->attrs;
  1590. struct ovs_header *ovs_header = info->userhdr;
  1591. struct sk_buff *reply;
  1592. struct vport *vport;
  1593. int err;
  1594. reply = ovs_vport_cmd_alloc_info();
  1595. if (!reply)
  1596. return -ENOMEM;
  1597. rcu_read_lock();
  1598. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1599. err = PTR_ERR(vport);
  1600. if (IS_ERR(vport))
  1601. goto exit_unlock_free;
  1602. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1603. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1604. BUG_ON(err < 0);
  1605. rcu_read_unlock();
  1606. return genlmsg_reply(reply, info);
  1607. exit_unlock_free:
  1608. rcu_read_unlock();
  1609. kfree_skb(reply);
  1610. return err;
  1611. }
  1612. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1613. {
  1614. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1615. struct datapath *dp;
  1616. int bucket = cb->args[0], skip = cb->args[1];
  1617. int i, j = 0;
  1618. rcu_read_lock();
  1619. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1620. if (!dp) {
  1621. rcu_read_unlock();
  1622. return -ENODEV;
  1623. }
  1624. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1625. struct vport *vport;
  1626. j = 0;
  1627. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1628. if (j >= skip &&
  1629. ovs_vport_cmd_fill_info(vport, skb,
  1630. NETLINK_CB(cb->skb).portid,
  1631. cb->nlh->nlmsg_seq,
  1632. NLM_F_MULTI,
  1633. OVS_VPORT_CMD_NEW) < 0)
  1634. goto out;
  1635. j++;
  1636. }
  1637. skip = 0;
  1638. }
  1639. out:
  1640. rcu_read_unlock();
  1641. cb->args[0] = i;
  1642. cb->args[1] = j;
  1643. return skb->len;
  1644. }
  1645. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1646. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1647. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1648. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1649. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1650. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1651. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1652. };
  1653. static const struct genl_ops dp_vport_genl_ops[] = {
  1654. { .cmd = OVS_VPORT_CMD_NEW,
  1655. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1656. .policy = vport_policy,
  1657. .doit = ovs_vport_cmd_new
  1658. },
  1659. { .cmd = OVS_VPORT_CMD_DEL,
  1660. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1661. .policy = vport_policy,
  1662. .doit = ovs_vport_cmd_del
  1663. },
  1664. { .cmd = OVS_VPORT_CMD_GET,
  1665. .flags = 0, /* OK for unprivileged users. */
  1666. .policy = vport_policy,
  1667. .doit = ovs_vport_cmd_get,
  1668. .dumpit = ovs_vport_cmd_dump
  1669. },
  1670. { .cmd = OVS_VPORT_CMD_SET,
  1671. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1672. .policy = vport_policy,
  1673. .doit = ovs_vport_cmd_set,
  1674. },
  1675. };
  1676. struct genl_family dp_vport_genl_family = {
  1677. .id = GENL_ID_GENERATE,
  1678. .hdrsize = sizeof(struct ovs_header),
  1679. .name = OVS_VPORT_FAMILY,
  1680. .version = OVS_VPORT_VERSION,
  1681. .maxattr = OVS_VPORT_ATTR_MAX,
  1682. .netnsok = true,
  1683. .parallel_ops = true,
  1684. .ops = dp_vport_genl_ops,
  1685. .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
  1686. .mcgrps = &ovs_dp_vport_multicast_group,
  1687. .n_mcgrps = 1,
  1688. };
  1689. static struct genl_family * const dp_genl_families[] = {
  1690. &dp_datapath_genl_family,
  1691. &dp_vport_genl_family,
  1692. &dp_flow_genl_family,
  1693. &dp_packet_genl_family,
  1694. };
  1695. static void dp_unregister_genl(int n_families)
  1696. {
  1697. int i;
  1698. for (i = 0; i < n_families; i++)
  1699. genl_unregister_family(dp_genl_families[i]);
  1700. }
  1701. static int dp_register_genl(void)
  1702. {
  1703. int err;
  1704. int i;
  1705. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1706. err = genl_register_family(dp_genl_families[i]);
  1707. if (err)
  1708. goto error;
  1709. }
  1710. return 0;
  1711. error:
  1712. dp_unregister_genl(i);
  1713. return err;
  1714. }
  1715. static int __net_init ovs_init_net(struct net *net)
  1716. {
  1717. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1718. INIT_LIST_HEAD(&ovs_net->dps);
  1719. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1720. return 0;
  1721. }
  1722. static void __net_exit ovs_exit_net(struct net *net)
  1723. {
  1724. struct datapath *dp, *dp_next;
  1725. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1726. ovs_lock();
  1727. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1728. __dp_destroy(dp);
  1729. ovs_unlock();
  1730. cancel_work_sync(&ovs_net->dp_notify_work);
  1731. }
  1732. static struct pernet_operations ovs_net_ops = {
  1733. .init = ovs_init_net,
  1734. .exit = ovs_exit_net,
  1735. .id = &ovs_net_id,
  1736. .size = sizeof(struct ovs_net),
  1737. };
  1738. static int __init dp_init(void)
  1739. {
  1740. int err;
  1741. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1742. pr_info("Open vSwitch switching datapath\n");
  1743. err = action_fifos_init();
  1744. if (err)
  1745. goto error;
  1746. err = ovs_internal_dev_rtnl_link_register();
  1747. if (err)
  1748. goto error_action_fifos_exit;
  1749. err = ovs_flow_init();
  1750. if (err)
  1751. goto error_unreg_rtnl_link;
  1752. err = ovs_vport_init();
  1753. if (err)
  1754. goto error_flow_exit;
  1755. err = register_pernet_device(&ovs_net_ops);
  1756. if (err)
  1757. goto error_vport_exit;
  1758. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1759. if (err)
  1760. goto error_netns_exit;
  1761. err = dp_register_genl();
  1762. if (err < 0)
  1763. goto error_unreg_notifier;
  1764. return 0;
  1765. error_unreg_notifier:
  1766. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1767. error_netns_exit:
  1768. unregister_pernet_device(&ovs_net_ops);
  1769. error_vport_exit:
  1770. ovs_vport_exit();
  1771. error_flow_exit:
  1772. ovs_flow_exit();
  1773. error_unreg_rtnl_link:
  1774. ovs_internal_dev_rtnl_link_unregister();
  1775. error_action_fifos_exit:
  1776. action_fifos_exit();
  1777. error:
  1778. return err;
  1779. }
  1780. static void dp_cleanup(void)
  1781. {
  1782. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1783. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1784. unregister_pernet_device(&ovs_net_ops);
  1785. rcu_barrier();
  1786. ovs_vport_exit();
  1787. ovs_flow_exit();
  1788. ovs_internal_dev_rtnl_link_unregister();
  1789. action_fifos_exit();
  1790. }
  1791. module_init(dp_init);
  1792. module_exit(dp_cleanup);
  1793. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1794. MODULE_LICENSE("GPL");